FraserOfSmeg
FraserOfSmeg

Reputation: 1148

How to make an array of matrix (of varying sizes)

I'm trying to get to grips with matlab, so this question is more about syntax than anything else.

I want to create a vector (1xn) of matrices. The matrices are all possibly of different dimensions eg. matrix 1 = 4 x 5, matrix 2 = 5 x 6 etc.

I tried using a for loop, but I had the following error:

Subscripted assignment dimension mismatch.

Upvotes: 1

Views: 132

Answers (1)

Teddy Ort
Teddy Ort

Reputation: 766

You can store an array of matrices of different sizes as a cell array of matrices. Often you'll want to create these cell arrays dynamically using the arrayfun function which will do this for you if you set the UniformOutput option to 0.

Example:

cols = [4 5 6];
rows = [1 2 3];
A = arrayfun(@(i) zeros(rows(i),cols(i)),1:3,'UniformOutput',0);
A{:}

Outputs:

ans =

     0     0     0     0


ans =

     0     0     0     0     0
     0     0     0     0     0


ans =

     0     0     0     0     0     0
     0     0     0     0     0     0
     0     0     0     0     0     0

Upvotes: 1

Related Questions