Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Intialization of an 2d array.

Subject: Intialization of an 2d array.

From: meena rao

Date: 15 May, 2011 01:29:04

Message: 1 of 3

Hi

How do i intialize a 2d array with zeros if the dimension of the array keep changing based on some data.

Can i do Arrayname=0; Is this right.

regards
meena

Subject: Intialization of an 2d array.

From: ImageAnalyst

Date: 15 May, 2011 02:22:12

Message: 2 of 3

 meena:
No, that's not right. Pass in the dimensions in as the variables
which keep changing every time you want to perform an initialization:

Arrayname = zeros(dim1Length, dim2Length);

Subject: Intialization of an 2d array.

From: Roger Stafford

Date: 15 May, 2011 02:58:04

Message: 3 of 3

"meena rao" <meenaraos@yahoo.co.in> wrote in message <iqna90$621$1@newscl01ah.mathworks.com>...
> Hi
>
> How do i intialize a 2d array with zeros if the dimension of the array keep changing based on some data.
>
> Can i do Arrayname=0; Is this right.
>
> regards
> meena
- - - - - - - - -
  There is no point whatever in writing Arrayname=0 just to allocate space for it. The purpose behind writing something like

 x = zeros(n,1);

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.)

  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.

  This is a frequent problem when using for-loops or while-loops 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.

  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.

  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.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us