M = rand(6,8);
new_row_order = [6;4;5;1;2;3];
M_new = M(new_row_order, :);
Reordering Matrix Rows in Matlab
Вопрос
I have a 6 x 8 matrix in matlab and would like to reorder the rows. Is there an easy way for me to shift around specific rows?
For my purposes, I would like to take initial rows 1, 2, and 3, and reorder them as rows 4, 5, and 6. Initial row 4 should be row 2, row 5 should be row 3, and row 6 should be row 1.
Thanks!
Решение
Другие советы
Yes. You can index the original matrix in the following way:
P = A([6 4 5], :)
Where A
is the original matrix and P
is the permuted matrix.
This means that the rows of P will be the 6th row of A, the 4th row of A and finally the 5th row of A.
Не связан с StackOverflow