Hualius
Hualius

Reputation: 11

Changing order of matrices in matlab

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

Answers (2)

ThomasIsCoding
ThomasIsCoding

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

MichaelTr7
MichaelTr7

Reputation: 4757

Collection Of Matrices Stored in Cell Arrays:

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.

Cell Array Reordering

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

Collection is a Set of Concatenated Matrices:

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.

Reordering Concatenated Matrices

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

Related Questions