1

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?

Community
  • 1
  • 1
seli
  • 67
  • 9
  • 1
    [This answer](http://stackoverflow.com/q/466972#467199) shows you how to add another matrix to your 3-D matrix collection. You can also use [cell arrays](http://www.mathworks.com/help/matlab/cell-arrays.html) instead, but that would be less efficient. – Eitan T May 30 '13 at 12:07
  • 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 May 30 '13 at 12:09
  • @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 May 30 '13 at 12:17
  • For cell arrays, look at [this answer](http://stackoverflow.com/q/466972#467042) 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 May 30 '13 at 12:21
  • @EitanT: all matrices are of equal size – seli May 30 '13 at 12:36
  • So use a 3D matrix like suggested in the first answer to the question you linked. – Eitan T May 30 '13 at 12:41
  • and I would like to exercise with arrays of matrices. – seli May 30 '13 at 12:45
  • So use the second answer then, and again, don't be paranoid about loops. – Eitan T May 30 '13 at 13:06

1 Answers1

1

You can also use mat2cell:

mat2cell(matrix, [4 4])
p8me
  • 1,840
  • 1
  • 15
  • 23
  • This won't work because the rows of each sub-matrix are not successive. You'll have to reshape the original matrix at least. – Eitan T May 30 '13 at 13:06
  • @EitanT I'm not sure what you mean. Well this splits the matrix along its first dimension to a `4x3` and another `4x3`, right? – p8me May 30 '13 at 13:12
  • I think all values in the first column of each matrix should be equal. So the first matrix should comprise all the rows with "1" in the first column, the second matrix should comprise all the rows with "2" in the first column, _etc_... – Eitan T May 30 '13 at 13:14
  • I tried with this and it works because the rows of each sub-matrix are successive (I reshaped the original matrix). Thanks! – seli May 30 '13 at 13:45