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 个评论
dpb
2019-7-5
Well, without some logic to specify which are the ones to change and what to change to, what are we to do???
采纳的回答
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
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
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 个评论
the cyclist
2019-7-5
OK, sorry. I missed the part about the varying values. See dpb's answer, which I think does what you want.
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
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)
% Replace column 1 with column 2:
M(:,1) = M(:,2)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!