1

I have to generate 20 random matrices, of increasing sizes

200:50:1150 //sizes of 20 random matrices

I want to store them in like an array of matrices:

like

array(1) // should give me the 1st matrix of size 200x200
array(2) // should give me the 2nd matrix of size 250x250 and so on

I am not sure how to do this:

n = 200:50:1150
for i=1:20
  M(:,:,i) = rand(n(i));   //This does not work
end

How do I do this, is there a faster way without loops?

2
  • The code example is not working. Please either fix it or describe what output you expect. Commented Nov 14, 2015 at 12:58
  • Here you go I edited the question Commented Nov 14, 2015 at 15:38

1 Answer 1

1

You can not stack your matrices of different sizes into a 3d matrix, a matrix has fixed dimensions. Use a cell array instead:

n = 200:50:1150;
M=cell(1,numel(n));
for ix=1:numel(n);
  M{ix} = rand(n(ix)); 
end

Not using a for loop won't increase the performance. Simply generating the same amount of random numbers in a single call takes the same time: rand(sum(n.^2),1);

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.