From: hank scorpio on
hello,

say I have a cell array that is dimensioned (1 x n) with each cell containing an array dimensioned (a x b x c, 'uint8')

What is the syntax for preallocating the arrays to memory with zeros then the cell array with the zero arrays before I fill it up all up with magic dust in a loop ?

regards,
hank
From: Bruno Luong on
"hank scorpio" <breakbeak(a)hotmail.com> wrote in message <hqjgpq$qrm$1(a)fred.mathworks.com>...
> hello,
>
> say I have a cell array that is dimensioned (1 x n) with each cell containing an array dimensioned (a x b x c, 'uint8')
>
> What is the syntax for preallocating the arrays to memory with zeros then the cell array with the zero arrays before I fill it up all up with magic dust in a loop ?
>

A proper way to preallocate arrays is to use for-loop or arrayfun

a=3; b=3; c=1;
n=4;
list=arrayfun(@(x) zeros(a,b,c,'uint8'),1:n,'uni',false);

% This assume later you fill with the subs index on the left-hand-side; such as

list{1}(:,:,:) = 255;

% Filling like this does not use preallocated arrays

list{1} = someimage;


% If you do this you'll see the array DO NOT SHARE the memory:
>> format debug
>> list{1}

ans =


Structure address = 7473908
m = 3
n = 3
pr = 1a3c6440
pi = 0
0 0 0
0 0 0
0 0 0

>> list{2}

ans =


Structure address = 7473908
m = 3
n = 3
pr = 1a3c8a60
pi = 0
0 0 0
0 0 0
0 0 0

% Two wrong practices:
% This is not proper way to allocate

>> list(1:n)={zeros([a b c],'uint8')};
>> list{1}

ans =


Structure address = 7473908
m = 3
n = 3
pr = 1a3c92a0
pi = 0
0 0 0
0 0 0
0 0 0

>> list{2} % shared

ans =


Structure address = 7473908
m = 3
n = 3
pr = 1a3c92a0
pi = 0
0 0 0
0 0 0
0 0 0

% This command is also ineffective for the same reason:

>> [list{1:n}]=deal(zeros([a b c],'uint8'));
>> list{1}

ans =


Structure address = 7473908
m = 3
n = 3
pr = 1a3c8210
pi = 0
0 0 0
0 0 0
0 0 0

>> list{2}

ans =


Structure address = 7473908
m = 3
n = 3
pr = 1a3c8210
pi = 0
0 0 0
0 0 0
0 0 0

>>

% Bruno