DSPRelated.com
Forums

matrix preallocation

Started by rosh...@gmail.com January 23, 2008
Hello,
Can someone please show me how to optimize this bit of code in matlab. I get a warning saying, \"consider preallocating \'rect\' for speed\". and the code is taking a loong time to execute.

%--------------------------
samples = 36;
m = randsrc(1,10000); % produces random -1\'s and 1\'s.
rect = []; % make empty matrix to store

for i = 1:length(m)
if m(i)== 1
temp1 = ones(1,samples); % fill in \'1\'s
elseif m(i) == -1
temp1 = ones(1,samples); % fill in \'-1\'s
temp1 = temp1*(-1);
end
rect = [rect temp1]; % combine the arrays
end

%--------------------------
thanks,

-Rosh
Hi,
This may not be the most elegant way of doing it, but it sure works. Here
goes:
%------------------------------
samples = 36;

%Add this
temp_store = []; %Initialize another temporary matrix to store

m = randsrc(1,10000); % produces random -1\'s and 1\'s.
rect = []; % make empty matrix to store

for i = 1:length(m)
if m(i)== 1
temp1 = ones(1,samples); % fill in \'1\'s
elseif m(i) == -1
temp1 = ones(1,samples); % fill in \'-1\'s
temp1 = temp1*(-1);
end

%changed this bit
rect = [temp_store temp1]; % combine the arrays
temp_store = rect;

end
%----------------------

Hope that helps,

Ekta
On Jan 22, 2008 12:04 PM, wrote:

> Hello,
> Can someone please show me how to optimize this bit of code in matlab. I
> get a warning saying, \"consider preallocating \'rect\' for speed\". and the
> code is taking a loong time to execute.
>
> %----------------------
> samples = 36;
> m = randsrc(1,10000); % produces random -1\'s and 1\'s.
> rect = []; % make empty matrix to store
>
> for i = 1:length(m)
> if m(i)== 1
> temp1 = ones(1,samples); % fill in \'1\'s
> elseif m(i) == -1
> temp1 = ones(1,samples); % fill in \'-1\'s
> temp1 = temp1*(-1);
> end
> rect = [rect temp1]; % combine the arrays
> end
>
> %----------------------
>
> thanks,
>
> -Rosh
>
Can you try this?
m = randsrc(1,10000); % produces random -1\'s and
1\'s.
rect = zeros(36,10000);
rect(:, m == -1) = -1;
rect(:, m == 1) = 1;
rect = rect(:)';

Amit

--- r...@gmail.com wrote:

> Hello,
> Can someone please show me how to optimize this bit
> of code in matlab. I get a warning saying,
> \"consider preallocating \'rect\' for speed\". and
> the code is taking a loong time to execute.
%--------------------------
> samples = 36;
> m = randsrc(1,10000); % produces random -1\'s and
> 1\'s.
> rect = []; % make empty matrix to store
>
> for i = 1:length(m)
> if m(i)== 1
> temp1 = ones(1,samples); % fill in \'1\'s
> elseif m(i) == -1
> temp1 = ones(1,samples); % fill in
> \'-1\'s
> temp1 = temp1*(-1);
> end
> rect = [rect temp1]; % combine the arrays
> end
%--------------------------
> thanks,
>
> -Rosh
close all, clear, clc;

samples = 36;
m = randsrc(1,10000); % produces random -1\'s and 1\'s.
% rect = []; % make empty matrix to store

rect=zeros(1,samples*length(m)); % replacement for line above

for i = 1:length(m)
if m(i)== 1
temp1 = ones(1,samples); % fill in \'1\'s
elseif m(i) == -1
temp1 = ones(1,samples); % fill in \'-1\'s
temp1 = temp1*(-1);
end
rect(((i-1)*samples+1):i*samples)=temp1; %replacement for line below
% rect = [rect temp1]; % combine the arrays
end

just calculate needed size, and allocate once with zeros() or ones()
code above takes about 1sec to execute, I think
Dear Rosh

In this case you have not defined the size of 'rect' ...
Matlab does automatic resizing of arrays ...Therefore, it takes longer time
to execute if the resizing occurs continually inside a '*For'* loop....

One of the solution to tackle this problem is to preallocate the array as
given below ..
*rect = zeros(1,36000);
*
Regards
S Prajit Nair
*
*

On Jan 23, 2008 4:04 AM, wrote:

> Hello,
> Can someone please show me how to optimize this bit of code in matlab. I
> get a warning saying, \"consider preallocating \'rect\' for speed\". and the
> code is taking a loong time to execute.
>
> %----------------------
> samples = 36;
> m = randsrc(1,10000); % produces random -1\'s and 1\'s.
> rect = []; % make empty matrix to store
>
> for i = 1:length(m)
> if m(i)== 1
> temp1 = ones(1,samples); % fill in \'1\'s
> elseif m(i) == -1
> temp1 = ones(1,samples); % fill in \'-1\'s
> temp1 = temp1*(-1);
> end
> rect = [rect temp1]; % combine the arrays
> end
>
> %----------------------
>
> thanks,
>
> -Rosh
>
>
>
Check out which piece of code runs faster on your
system:

m = randsrc(1,10000) ; % produces random -1\'s and
1\'s.
rect = zeros(36,10000);
rect(:, m == -1) = -1;
rect(:, m == 1) = 1;
rect = rect(:)';

OR

m = randsrc(1,10000); % produces random -1\'s and
1\'s.
rect = repmat(m,36,1);
rect = rect(:)';

Amit

--- r...@gmail.com wrote:

> Hello,
> Can someone please show me how to optimize this bit
> of code in matlab. I get a warning saying,
> \"consider preallocating \'rect\' for speed\". and
> the code is taking a loong time to execute.
%--------------------------
> samples = 36;
> m = randsrc(1,10000); % produces random -1\'s and
> 1\'s.
> rect = []; % make empty matrix to store
>
> for i = 1:length(m)
> if m(i)== 1
> temp1 = ones(1,samples); % fill in \'1\'s
> elseif m(i) == -1
> temp1 = ones(1,samples); % fill in
> \'-1\'s
> temp1 = temp1*(-1);
> end
> rect = [rect temp1]; % combine the arrays
> end
%--------------------------
> thanks,
>
> -Rosh
Prajit,

Preallocating rect =zeros(1,36000) does not work in this particular case
because he appends to his matrix in each iteration. ie.,
rect = [rect, temp1] % combine the arrays.
Preallocating to zeros in this case will start appending to a zero(1,36000)
matrix, and not to an empty [] matrix which is what he wants.

Ekta

On Jan 23, 2008 4:48 PM, PRAJIT S NAIR wrote:

> Dear Rosh
>
> In this case you have not defined the size of 'rect' ...
> Matlab does automatic resizing of arrays ...Therefore, it takes longer
> time to execute if the resizing occurs continually inside a ' *For'*loop....
>
> One of the solution to tackle this problem is to preallocate the array as
> given below ..
> *rect = zeros(1,36000);
> *
> Regards
> S Prajit Nair
> *
> *
>
> On Jan 23, 2008 4:04 AM, wrote:
>
> > Hello,
> > Can someone please show me how to optimize this bit of code in matlab. I
> > get a warning saying, \"consider preallocating \'rect\' for speed\". and the
> > code is taking a loong time to execute.
> >
> > %----------------------
> > samples = 36;
> > m = randsrc(1,10000); % produces random -1\'s and 1\'s.
> > rect = []; % make empty matrix to store
> >
> > for i = 1:length(m)
> > if m(i)== 1
> > temp1 = ones(1,samples); % fill in \'1\'s
> > elseif m(i) == -1
> > temp1 = ones(1,samples); % fill in \'-1\'s
> > temp1 = temp1*(-1);
> > end
> > rect = [rect temp1]; % combine the arrays
> > end
> >
> > %----------------------
> >
> > thanks,
> >
> > -Rosh
> >
> >
Thanks,

i used the kron function operator. it seems alot faster.
regards,
Rosh

Hello,
>Can someone please show me how to optimize this bit of code in matlab. I get a warning saying, \"consider preallocating \'rect\' for speed\". and the code is taking a loong time to execute.
>
>%--------------------------
>samples = 36;
>m = randsrc(1,10000); % produces random -1\'s and 1\'s.
>rect = []; % make empty matrix to store
>
>for i = 1:length(m)
> if m(i)== 1
> temp1 = ones(1,samples); % fill in \'1\'s
> elseif m(i) == -1
> temp1 = ones(1,samples); % fill in \'-1\'s
> temp1 = temp1*(-1);
> end
> rect = [rect temp1]; % combine the arrays
>end
>
>%--------------------------
>thanks,
>
>-Rosh
>
Hello Ekta

I agree with you but it also works ...
Preallocating array helps in speeding up the operation.. If there is any
problem in determining size of the array then he can use an upper bound
for the array ........The excess array size can be erased after the 'for'
loop

Also he can use "profile" command to check the execution time which will
help him in optimizing the code....Basically It tell which function/routine
is consuming maximum time....
Thanks and Regards
Prajit S Nair
On Jan 25, 2008 1:07 AM, Ekta Jain wrote:

> Prajit,
>
> Preallocating rect =zeros(1,36000) does not work in this particular case
> because he appends to his matrix in each iteration. ie.,
> rect = [rect, temp1] % combine the arrays.
> Preallocating to zeros in this case will start appending to a
> zero(1,36000) matrix, and not to an empty [] matrix which is what he wants.
>
> Ekta
>
> On Jan 23, 2008 4:48 PM, PRAJIT S NAIR wrote:
>
> > Dear Rosh
> >
> > In this case you have not defined the size of 'rect' ...
> > Matlab does automatic resizing of arrays ...Therefore, it takes longer
> > time to execute if the resizing occurs continually inside a ' *For'*loop....
> >
> > One of the solution to tackle this problem is to preallocate the array
> > as given below ..
> > *rect = zeros(1,36000);
> > *
> >
> >
> > Regards
> > S Prajit Nair
> > *
> > *
> >
> > On Jan 23, 2008 4:04 AM, wrote:
> >
> > > Hello,
> > > Can someone please show me how to optimize this bit of code in matlab.
> > > I get a warning saying, \"consider preallocating \'rect\' for speed\". and
> > > the code is taking a loong time to execute.
> > >
> > > %----------------------
> > > samples = 36;
> > > m = randsrc(1,10000); % produces random -1\'s and 1\'s.
> > > rect = []; % make empty matrix to store
> > >
> > > for i = 1:length(m)
> > > if m(i)== 1
> > > temp1 = ones(1,samples); % fill in \'1\'s
> > > elseif m(i) == -1
> > > temp1 = ones(1,samples); % fill in \'-1\'s
> > > temp1 = temp1*(-1);
> > > end
> > > rect = [rect temp1]; % combine the arrays
> > > end
> > >
> > > %----------------------
> > >
> > > thanks,
> > >
> > > -Rosh
> > >
> > >
> >
> >