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

37 次查看(过去 30 天)
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 个评论
SNT
SNT 2019-7-5
编辑:SNT 2019-7-5
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!

请先登录,再进行评论。

采纳的回答

dpb
dpb 2019-7-5
编辑:dpb 2019-7-8
"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 个评论
dpb
dpb 2019-7-8
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... :)

请先登录,再进行评论。

更多回答(2 个)

the cyclist
the cyclist 2019-7-5
编辑:the cyclist 2019-7-5
If your original array is A, then I think
A(mod(A,10)==1)=1;
will do what you want.
  3 个评论
dpb
dpb 2019-7-5
编辑:dpb 2019-7-5
"...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...

请先登录,再进行评论。


Soumyadeep Das
Soumyadeep Das 2022-3-17
how do i normally convert any coloumn number like replacing column 1 with column 2 of a matrix
  1 个评论
the cyclist
the cyclist 2022-3-17
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by