1

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.

1
  • 4
    You can use cell arrays Commented Mar 26, 2017 at 3:34

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.