How to copy first column, and replace other columns with this first column values in same array in MATLAB? (array size can vary)
27 views (last 30 days)
Show older comments
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
dpb
on 5 Jul 2019
Well, without some logic to specify which are the ones to change and what to change to, what are we to do???
Accepted Answer
dpb
on 5 Jul 2019
Edited: dpb
on 8 Jul 2019
"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
dpb
on 8 Jul 2019
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... :)
More Answers (2)
the cyclist
on 5 Jul 2019
Edited: 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
the cyclist
on 5 Jul 2019
OK, sorry. I missed the part about the varying values. See dpb's answer, which I think does what you want.
Soumyadeep Das
on 17 Mar 2022
how do i normally convert any coloumn number like replacing column 1 with column 2 of a matrix
1 Comment
the cyclist
on 17 Mar 2022
In the future, I would suggest opening a new question, not appending a comment to a 2-year-old question. But, I happened to see this, and it is simple, so I'll answer it here:
% Make up some data
M = magic(4)
% Replace column 1 with column 2:
M(:,1) = M(:,2)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!