How to copy first column, and replace other columns with this first column values in same array in MATLAB? (array size can vary)
33 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
SNT
el 5 de Jul. de 2019
Comentada: the cyclist
el 17 de Mzo. de 2022
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 comentarios
dpb
el 5 de Jul. de 2019
Well, without some logic to specify which are the ones to change and what to change to, what are we to do???
Respuesta aceptada
dpb
el 5 de Jul. de 2019
Editada: dpb
el 8 de Jul. de 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 comentarios
dpb
el 8 de Jul. de 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... :)
Más respuestas (2)
the cyclist
el 5 de Jul. de 2019
Editada: the cyclist
el 5 de Jul. de 2019
If your original array is A, then I think
A(mod(A,10)==1)=1;
will do what you want.
3 comentarios
the cyclist
el 5 de Jul. de 2019
OK, sorry. I missed the part about the varying values. See dpb's answer, which I think does what you want.
Soumyadeep Das
el 17 de Mzo. de 2022
how do i normally convert any coloumn number like replacing column 1 with column 2 of a matrix
1 comentario
the cyclist
el 17 de Mzo. de 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)
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!