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

29 views (last 30 days)
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
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???
SNT
SNT on 5 Jul 2019
Edited: SNT on 5 Jul 2019
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.

Accepted Answer

dpb
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
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... :)

Sign in to comment.

More Answers (2)

the cyclist
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
dpb
dpb on 5 Jul 2019
Edited: dpb on 5 Jul 2019
"...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.


Soumyadeep Das
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
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)
M = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
% Replace column 1 with column 2:
M(:,1) = M(:,2)
M = 4×4
2 2 3 13 11 11 10 8 7 7 6 12 14 14 15 1

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!