http://www.mathworks.com/matlabcentral/newsreader/view_thread/307766
MATLAB Central Newsreader  Intialization of an 2d array.
Feed for thread: Intialization of an 2d array.
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 15 May 2011 01:29:04 +0000
Intialization of an 2d array.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307766#836048
meena rao
Hi<br>
<br>
How do i intialize a 2d array with zeros if the dimension of the array keep changing based on some data.<br>
<br>
Can i do Arrayname=0; Is this right.<br>
<br>
regards <br>
meena

Sun, 15 May 2011 02:22:12 +0000
Re: Intialization of an 2d array.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307766#836049
ImageAnalyst
meena:<br>
No, that's not right. Pass in the dimensions in as the variables<br>
which keep changing every time you want to perform an initialization:<br>
<br>
Arrayname = zeros(dim1Length, dim2Length);

Sun, 15 May 2011 02:58:04 +0000
Re: Intialization of an 2d array.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307766#836051
Roger Stafford
"meena rao" <meenaraos@yahoo.co.in> wrote in message <iqna90$621$1@newscl01ah.mathworks.com>...<br>
> Hi<br>
> <br>
> How do i intialize a 2d array with zeros if the dimension of the array keep changing based on some data.<br>
> <br>
> Can i do Arrayname=0; Is this right.<br>
> <br>
> regards <br>
> meena<br>
        <br>
There is no point whatever in writing Arrayname=0 just to allocate space for it. The purpose behind writing something like<br>
<br>
x = zeros(n,1);<br>
<br>
where n is large is usually to allocate space in the memory for the array x which is to be filled in later, (though sometimes the zero values themselves are needed.)<br>
<br>
The whole idea is to *not* be changing the array's size frequently, since that introduces serious delays in computation while the matlab searches through the available memory for a sufficiently large contiguous unused space to which it then has to copy all the existing contents from the array at its previous location.<br>
<br>
This is a frequent problem when using forloops or whileloops where inexperienced programmers are tempted to extend the size of an array by one element each trip through the loop. Each time this happens this can cause the above type of disruption in memory usage. However if the array has previously been allocated to a large enough size that it can handle all the elements input to it, the computer can run much faster.<br>
<br>
There are sometimes situations where the total size needed for an array cannot be predicted in advance. In that case it is best to lay claim to enough space to allow for worst case needs, and while the array is being filled the programmer needs to keep track of the portion of the array that has been filled. When that process is ended, the unused portion can then be deleted.<br>
<br>
If some such strategy is not possible, the next best thing is to add on large hunks at a time to the array with concatenate operations when larger sizes are needed so as to at least minimize the time lost in relocated the array.<br>
<br>
Roger Stafford