8

I want to create an 1,k cell of m,m matrices. I have some trouble trying to initialize it. My first idea was to do this

myCell = cell{1,K};
for k = 1:K
  myCell{1,k} = eye(m);
end 

But it seems like such ugly way to initialize it. There have to be a better way?

4 Answers 4

5

A solution with even fewer function calls:

[myCell{1:k}] = deal(eye(m));
Sign up to request clarification or add additional context in comments.

Comments

3

Here's a very simple REPMAT solution:

myCell = repmat({eye(m)},1,K);

This simply creates one cell with eye(m) in it, then replicates that cell K times.

Comments

2

Try this:

myCell =  mat2cell(repmat(eye(m),[1 k]),[m],repmat(m,1,k))

Comments

2

Consider this one:

myCell = arrayfun(@(x)eye(m), 1:k, 'UniformOutput',false)

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.