Reputation: 11
In matlab I have a set of matrices e.g. ([1,2;3,4],[5,6;7,8],[9,10;11,12]) and I want to change the order of them e.g. ([5,6;7,8],[9,10;11,12],[1,2;3,4]), can I get some help into doing this?
Upvotes: 0
Views: 207
Reputation: 101064
Assuming your matrices are stored in a cell C
, e.g.,
C = {[1,2;3,4],[5,6;7,8],[9,10;11,12]}
you can use the following code to reorder the matrices
C([2,3,1])
Upvotes: 1
Reputation: 4757
If the matrices are stored in a cell array you can reorder them by indexing. Here the reordered matrix is stored in cell array Matrix_2
. The result can be set to the original by: Matrix_1 = Matrix_2
.
Matrix_1 = {[1,2;3,4],[5,6;7,8],[9,10;11,12]};
Matrix_1
Matrix_2(1) = Matrix_1(2);
Matrix_2(2) = Matrix_1(3);
Matrix_2(3) = Matrix_1(1);
Matrix_2
Similar to the previous indexing used for the cell arrays you can index the two dimensional matrix by its rows and columns. The numbers neighbouring the colon :
signifies the range of cells to take from start:end
.
Matrix_1 = [[1,2;3,4],[5,6;7,8],[9,10;11,12]];
Matrix_1
Matrix_2(1:2,1:2) = Matrix_1(1:2,3:4);
Matrix_2(1:2,3:4) = Matrix_1(1:2,5:6);
Matrix_2(1:2,5:6) = Matrix_1(1:2,1:2);
Matrix_2
Ran using MATLAB R2019b
Upvotes: 0