MATLAB Answers

SNT
0

How to copy first column, and replace other columns with this first column values in same array in MATLAB? (array size can vary)

Asked by SNT
on 5 Jul 2019
Latest activity Commented on by SNT
on 8 Jul 2019
Accepted Answer by dpb
Hello everyone,
I have an array with many columns and rows; as an example see below. The data is arranged as 4 columns per each set, and repetes as bundles of 4.
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
Now I would like to take the first column, and replace following columns with this column as below;
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
The column and row numbers in my actual data can vary in number, and so the syntax must be able to handle this changing rows and columns, while replacing the columns with the first column.
Would be wonderful if some one could help me out with this!
Thanks in advance
ST

  2 Comments

Well, without some logic to specify which are the ones to change and what to change to, what are we to do???
Hi dpb. Thanks for your reply. Actually I want to copy the first column (column of 1 s) in this array, and use this (column of 1 s) to replace the column of 11 s, and the column of 111, and the column with 1111 s, and so on. while all other columns are untouched. I tried to show this by showing the array I would like to get as out put. I have put both the input array and the output array in my question. I hope it is clear? Please do let me know if i am not explaining correctly.
Thanks again!

Sign in to comment.

2 Answers

Answer by dpb
on 5 Jul 2019
Edited by dpb
on 8 Jul 2019
 Accepted Answer

"copy what ever is in the first column (first column will have each row with different values and have n number of rows in it), and replace the 5th column, 9th column, 13th column etc till all columns are over."
Why didn't you just say that in the first place?
A(:,5:4:end)=repmat(A(:,1),1,numel(A(1,5:4:end)));
As far as the colon expression see doc colon and then experiment at the command line to see what you get...nothing will blow up, I promise (other than you may see a lot of numbers if A is very large, so you might want to play with small sample arrays for ease)

  3 Comments

Hello dpb! Thanks so much for your reply. I ran the code, however I get the below error
Subscripted assignment dimension mismatch.
How may I adjust this? I don't quite understand how I can modify the code section, as I don't quite know how the 5:4:end part works. Extremely sorry about my minimum knowledge.
Thank a lot in advance!
ST
Oh, dang! That's one of those expressions that looks like it should work by automgic expansion but doesn't...my bad.
See updated Answer for the straightahead fix...there's probably a more clever workaround but it's late and I'm tired... :)
Hello dpb! Thanks a lot! This works great :) Good night!

Sign in to comment.


Answer by the cyclist
on 5 Jul 2019
Edited by the cyclist
on 5 Jul 2019

If your original array is A, then I think
A(mod(A,10)==1)=1;
will do what you want.

  3 Comments

Hi cyclist! Thank for the reply. This works well for the set of 10 rows. However, my data can have any number of rows and columns in it (not just ten). Also, the first column with the 1 s that I have shown is just an example, and can have any values in it. So what I would like to do is simply copy what ever is in the first column (first column will have each row with different values and have n number of rows in it), and replace the 5th column, 9th column, 13th column etc till all columns are over.
Would be super if you could help me out. Thanks a lot!
I shall try to illustrate below;
so the input will be
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
4 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
7 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
4 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
3 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
6 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
7 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
8 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
2 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
1 2 3 4 11 22 33 44 111 222 333 444 1111 2222 3333 4444
and the output will be
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
4 2 3 4 4 22 33 44 4 222 333 444 4 2222 3333 4444
7 2 3 4 7 22 33 44 7 222 333 444 7 2222 3333 4444
4 2 3 4 4 22 33 44 4 222 333 444 4 2222 3333 4444
3 2 3 4 3 22 33 44 3 222 333 444 3 2222 3333 4444
6 2 3 4 6 22 33 44 6 222 333 444 6 2222 3333 4444
7 2 3 4 7 22 33 44 7 222 333 444 7 2222 3333 4444
8 2 3 4 8 22 33 44 8 222 333 444 8 2222 3333 4444
2 2 3 4 2 22 33 44 2 222 333 444 2 2222 3333 4444
1 2 3 4 1 22 33 44 1 222 333 444 1 2222 3333 4444
OK, sorry. I missed the part about the varying values. See dpb's answer, which I think does what you want.
"...missed the part about the varying values"
Can't see why...<not!>
Had no basis for any code at all initially except to duplicate numbers which yours did just as well...

Sign in to comment.