0

I'm trying to create a cell array of size N, where every cell is a randomized Matrix of size M, I've tried using deal or simple assignments, but the end result is always N identical Matrices of size M for example:

N=20;
M=10;
CellArray=cell(1,N);
CellArray(1:20)={rand(M)};

this yields identical matrices in each cell, iv'e tried writing the assignment like so:

CellArray{1:20}={rand(M)};

but this yields the following error:

The right hand side of this assignment has too few values to satisfy the left hand side.

the ends results should be a set of transition probability matrices to be used for a model i'm constructing, there's a currently working version of the model, but it uses loops to create the matrices, and works rather slowly, i'd be thankful for any help

2
  • 1
    what's wrong with the loop solution? Surely, this is not the performance-critical section of the code... Commented Apr 11, 2014 at 15:12
  • I'm not sure, this isn't the actual code, and the actual code needs to generate 500 of these, and then create some additional variables bases on these matrices Commented Apr 11, 2014 at 15:18

2 Answers 2

2

If you don't want to use loops because you are interested in a low execution time, get rid of the cells.

RandomArray=rand(M,M,N)

You can access each slice, which is your intended MxM matrix, using RandomArray(:,:,index)

Sign up to request clarification or add additional context in comments.

2 Comments

This sounds like a great solution! ill give it a try
@ronimb And if you really need a cell array: add RandomArray = mat2cell(RandomArray,M,M,ones(1,N)); at the end
1

Use cellfun:

N = 20;
M = 10;

CellArray = cellfun(@(x) rand(M), cell(1,N), 'uni',0)

For every cell it newly calls rand(M) - unlike before, you were assigning the same rand(M) to every cell, which was just computed once.

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.