Switching values around in a matrix
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Scott Banks
el 1 de Ag. de 2025
Comentada: dpb
el 2 de Ag. de 2025
Hi,
Say I have a 5 by 2 matrix in the form:
A = [2 5; 9 7; 10 2; 3 2; 1 9]
And I want to make a 10 by 1 matrix from these values so that I get:
B = [2;5;9;7;10;2;3;2;1;9]
How would I do this?
I know there is a probably a simple fix, but I haven't been able to do it.
Many thanks,
Scott
Respuesta aceptada
Matt J
el 1 de Ag. de 2025
A = [2 5; 9 7; 10 2; 3 2; 1 9];
B([1:2:10,2:2:10],1)=A(:)
3 comentarios
dpb
el 2 de Ag. de 2025
@Scott Banks, you're free to choose the solution you wish, but I'm curious why you chose the direct indexing solution over the generic one?
As I noted, it does illustrate using vectors as indices in MATLAB which is a powerful tool/feature and is sometimes invaluable, but the other code is general for any size array rather than only working for the specific case and more difficult to code generically (as my other tongue-in-cheek examples illustrate).
Again, just wondering what was your thinking here? Did my response need more amplification beyond the comments?
Más respuestas (1)
dpb
el 1 de Ag. de 2025
Editada: dpb
el 1 de Ag. de 2025
A = [2 5; 9 7; 10 2; 3 2; 1 9];
% option A
B=A.'; B=B(:) % B=A.'(:); is invalid MATLAB syntax, unfortunately. (Octave allows this in at least some contexts)
% option B
B=reshape(A.',[],1) % how to implement the above wanted but unallowable syntax...
The key is to recognize need to transpose to get in needed/wanted column order first...
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!