help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: preallocating memory for cell arrays


From: John W. Eaton
Subject: Re: preallocating memory for cell arrays
Date: Fri, 5 Nov 2004 14:10:03 -0500

On  5-Nov-2004, E. Joshua Rigler <address@hidden> wrote:

| Actually, it was a "structure array" that I wanted in the first place,
| but I couldn't figure out how to make one.  For instance, this certainly
| doesn't work in 2.1.57:
| 
|   x=struct('test1',zeros(2));
|   X=[x,x];
| 
| Using a cell array (i.e. X={x,x}) was just a stop-gap solution as far as
| I was concerned, but I could find nothing in any of the documentation on
| how to create an array of structures.
| 
| Then, as I was about to click send for this email, the obvious solution
| hit me like a ton of bricks.  I'll share it here for all those who may
| have been as dense as I about this particular problem:
| 
|   x=struct('test2',zeros(2));
|   X(1:2) = x;
|  
| So, 1) is this the best solution; 2) does this actually preallocate all
| the memory I will need; and 3) would the syntax "X=[x,x]" ever be
| desirable (see first example above)?  Thanks.

Yes, concatenation of structures works in 2.1.60.

Also, you can do things like

  s(1:N).field1 = zeros (nr1, nc2);
  s(1:N).field2 = zeros (nr2, nc2);
  ...

but your method of using struct might look better if you have multiple
fields to initialize.  You'd have to do some timings to see which is
faster.

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]