开发者

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
开发者_如何学Go  myCell{1,k} = eye(m);
end 

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


A solution with even fewer function calls:

[myCell{1:k}] = deal(eye(m));


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.


Try this:

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


Consider this one:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜