1
votes

I am still a newbe and I have probabily a very easy question concerning arrays of matrices. I have a matrix of nrows like the following one:

>> matrix
 1 678 543
 2 676 541
 3 543 987
 4 543 98
 1 433 54
 2 908 32
 3 457 54
 4 235 21

How to create arrays of equal size matrices? i.e array{i,1}

This is replication of question: Array of Matrices in MATLAB and probably of many others.

What is not clear to me, is how to populate my array of fixed dimension matrices. So that

>>array{1,1}
1 678 543
2 676 541
3 543 987
4 543 98

Here is my attempt:
Find all the ones in column 1 of matrix and the size of matrix. Create cell arrays, look in each line, if it is equal to 1 create an array{i,1} of zeros equal to the size of the matrices I want to create (in my case 4x3).
If not equal to 1 insert into the array the first four values of matrix.

Is there any faster way to do it without a loop?

1
This answer shows you how to add another matrix to your 3-D matrix collection. You can also use cell arrays instead, but that would be less efficient.Eitan T
I dont know if that's what you like but if you do like array = matrix(1,:), it will copy all the elements in the 1st line to an array.Akatosh
@EitanT: thanks but I would like to use cell arrays in order to be able to change the code in case I will have different dimension matrices.seli
For cell arrays, look at this answer then. There's nothing wrong with a loop (start optimizing your code only if you run into performance issues). But I don't understand one thing: if you want your collection to store matrices of variable dimensions, why did you state that your matrices have fixed dimensions? Be clear with your question.Eitan T
@EitanT: all matrices are of equal sizeseli

1 Answers

1
votes

You can also use mat2cell:

mat2cell(matrix, [4 4])