Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create cell-array in MATLAB and initialize all elements to the same object?

I have a matrix (call it X) that is initialized to say zero(3).

I want to change the code so that X is a cell array of size (say) (3,1) and initialize each element to zero(3).

I can do it with a loop but is there a better way?

X = cell(3,1);
for ii=1:numel(X)
    X{ii} = zeros(3);
end
like image 707
s5s Avatar asked Nov 19 '11 16:11

s5s


1 Answers

You can do this with deal().

>> [X{1:3, 1}] = deal(zeros(3))

X = 

    [3x3 double]
    [3x3 double]
    [3x3 double]
like image 172
John Colby Avatar answered Oct 11 '22 07:10

John Colby