Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a cell array of k similar objects in Matlab?

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?

like image 759
Reed Richards Avatar asked May 30 '10 16:05

Reed Richards


3 Answers

A solution with even fewer function calls:

[myCell{1:k}] = deal(eye(m));
like image 52
Jonas Avatar answered Sep 19 '22 16:09

Jonas


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.

like image 36
gnovice Avatar answered Sep 20 '22 16:09

gnovice


Try this:

myCell =  mat2cell(repmat(eye(m),[1 k]),[m],repmat(m,1,k))
like image 24
High Performance Mark Avatar answered Sep 20 '22 16:09

High Performance Mark