Prev: Results not repeatable for sum() function on fixed / deterministic vector
Next: Results not repeatable for sum() function on fixed / deterministic vector
From: Dan on 5 Apr 2010 12:48 Hi, I know how to create an array of zeros i.e. if i type a=zeros(1,4) then a=0 0 0 0 however i've totally forgotten how to create an array with curly brackets, i.e. i want to create a={0,0,0,0} which is a=[0] [0] [0] [0] but i want to specify the amount of zeros i want. Any help would be appreciated, Thanks
From: us on 5 Apr 2010 13:03 "Dan " <drcscott(a)hotmail.co.uk> wrote in message <hpd484$hkc$1(a)fred.mathworks.com>... > Hi, > I know how to create an array of zeros i.e. if i type a=zeros(1,4) then a=0 0 0 0 > however i've totally forgotten how to create an array with curly brackets, i.e. i want to create a={0,0,0,0} which is a=[0] [0] [0] [0] but i want to specify the amount of zeros i want. > Any help would be appreciated, > Thanks one of the very many solutions a(1:4)={0} % a = [0] [0] [0] [0] us
From: Jan Simon on 5 Apr 2010 15:28 Dear Dan! Or: a = cell(1, 4); a(:) = {0}; Jan
From: Bruno Luong on 5 Apr 2010 16:00 Some ways that I could think of (restrict to not using for-loop) %% a{1,4}=0; a(:)={0} %% a=cell(1,4); a(:)={0} %% a(1:4)={0} %% [a{1:4}]=deal(0) %% a={0}; a=a(ones(1,4)) %% a=repmat({0},1,4) %% a=num2cell(zeros(1,4)) %% a=mat2cell(zeros(1,4),1,ones(1,4)) %% a=arrayfun(@(x) {x}, zeros(1,4)) % Bruno
From: James Tursa on 5 Apr 2010 16:48
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hpdfga$emf$1(a)fred.mathworks.com>... > > %% > a=num2cell(zeros(1,4)) > > %% > a=mat2cell(zeros(1,4),1,ones(1,4)) > > %% > a=arrayfun(@(x) {x}, zeros(1,4)) I would point out to OP that the last three methods listed by Bruno above do not share the 0 array among the elements like the other methods do, so I would not recommend them if you extend the method to much larger sized cell arrays. Better to use one of the other methods that uses shared arrays. I would also point out to OP that if you are doing this to preallocate the cell array with the intent to replace them later then don't bother. Once you do a{__} = something assignment then the 0 cell gets wiped from memory anyway. Best to just use the cell( ) function to create a desired size cell array with a bunch of NULL pointer cell elements and then fill them up later with your code. But if you are actually using the 0 value somewhere later in your code then pre-filling with 0 makes sense. James Tursa |