I have a structure array with two subarrays. One subarray
is an array of doubles, and the other is an array of
chars. I want to preallocate my structure array before I
add my data to it, so that I don't take a huge hit in speed.
My question is what would be the best way to preallocate my
strucutre array, especially if it has a subarray of chars?
In my subarray of chars, I'd like to store words of
different size (i.e. 'blue', 'green', 'red', etc). Also,
I'd like to preallocate a large amount of space for both of
my subarrays.
"C M" <cmikki17@yahoo.com> wrote in message
<g431s2$6fp$1@fred.mathworks.com>...
> Hi,
>
> I have a structure array with two subarrays. One subarray
> is an array of doubles, and the other is an array of
> chars. I want to preallocate my structure array before I
> add my data to it, so that I don't take a huge hit in speed.
>
> My question is what would be the best way to preallocate my
> strucutre array, especially if it has a subarray of chars?
> In my subarray of chars, I'd like to store words of
> different size (i.e. 'blue', 'green', 'red', etc). Also,
> I'd like to preallocate a large amount of space for both of
> my subarrays.
>
> Thank you for any help you may provide.
You could try something like this:
% max sizes for double subarray, char subarray, and overall
array
maxDoubleSize = 100;
maxCharSize = 100;
arrSize = 100;
% a representative element
element =
struct('arrDoubles',zeros(1,maxDoubleSize),'arrChars',char(zeros(1,maxCharSize)));
% your final array, which replicates the elements
data = repmat(element,1,100);
"Ian Clarkson" <ovoidkumquat@hotmail.com> wrote in message
<g433tm$uh$1@fred.mathworks.com>...
> "C M" <cmikki17@yahoo.com> wrote in message
> <g431s2$6fp$1@fred.mathworks.com>...
> > Hi,
> >
> > I have a structure array with two subarrays. One
subarray
> > is an array of doubles, and the other is an array of
> > chars. I want to preallocate my structure array before
I
> > add my data to it, so that I don't take a huge hit in
speed.
> >
> > My question is what would be the best way to
preallocate my
> > strucutre array, especially if it has a subarray of
chars?
> > In my subarray of chars, I'd like to store words of
> > different size (i.e. 'blue', 'green', 'red', etc).
Also,
> > I'd like to preallocate a large amount of space for
both of
> > my subarrays.
> >
> > Thank you for any help you may provide.
>
> You could try something like this:
>
> % max sizes for double subarray, char subarray, and
overall
> array
> maxDoubleSize = 100;
> maxCharSize = 100;
> arrSize = 100;
>
> % a representative element
> element =
> struct('arrDoubles',zeros(1,maxDoubleSize),'arrChars',char
(zeros(1,maxCharSize)));
>
> % your final array, which replicates the elements
> data = repmat(element,1,100);
> Hi,
>
> I have a structure array with two subarrays. One subarray
> is an array of doubles, and the other is an array of
> chars. I want to preallocate my structure array before I
> add my data to it, so that I don't take a huge hit in speed.
>
> My question is what would be the best way to preallocate my
> strucutre array, especially if it has a subarray of chars?
> In my subarray of chars, I'd like to store words of
> different size (i.e. 'blue', 'green', 'red', etc). Also,
> I'd like to preallocate a large amount of space for both of
> my subarrays.
You actually don't need to preallocate the contents of a struct array.
As long as you preallocate the struct array itself (with the correct
size), with empty fields, you'll be fine. Also preallocate each
subarray before writing to it in a loop, but you can do that when you
need it.
Entries in a cell/struct array are simply more MATLAB variables, so
adding an element to a cell/struct (as long as the cell/struct is not
expanding itself) does not require reallocating or copying data.
% This is important:
a = repmat(struct('field1', [], 'field2', []), 100, 1);
for i = 1:100
% This is fine:
a(i).field1 = zeros(100);
a(i).field2 = 'blue';
% This is not fine:
for j = 1:200
a(i).field1(j) = j;
end
% But this is fine:
a(i).field1 = zeros(200,1);
for j = 1:200
a(i).field1(j) = j;
end
Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muyiqvu25jd.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "C M" <cmikki17@yahoo.com> writes:
>
> > Hi,
> >
> > I have a structure array with two subarrays. One
subarray
> > is an array of doubles, and the other is an array of
> > chars. I want to preallocate my structure array before
I
> > add my data to it, so that I don't take a huge hit in
speed.
> >
> > My question is what would be the best way to
preallocate my
> > strucutre array, especially if it has a subarray of
chars?
> > In my subarray of chars, I'd like to store words of
> > different size (i.e. 'blue', 'green', 'red', etc).
Also,
> > I'd like to preallocate a large amount of space for
both of
> > my subarrays.
>
> You actually don't need to preallocate the contents of a
struct array.
> As long as you preallocate the struct array itself (with
the correct
> size), with empty fields, you'll be fine. Also
preallocate each
> subarray before writing to it in a loop, but you can do
that when you
> need it.
>
> Entries in a cell/struct array are simply more MATLAB
variables, so
> adding an element to a cell/struct (as long as the
cell/struct is not
> expanding itself) does not require reallocating or
copying data.
>
> % This is important:
> a = repmat(struct('field1', [], 'field2', []), 100, 1);
>
> for i = 1:100
> % This is fine:
> a(i).field1 = zeros(100);
> a(i).field2 = 'blue';
>
> % This is not fine:
> for j = 1:200
> a(i).field1(j) = j;
> end
>
> % But this is fine:
> a(i).field1 = zeros(200,1);
> for j = 1:200
> a(i).field1(j) = j;
> end
>
> end
>
>
> Does that make sense?
>
>
> -Peter
Tags for this Thread
Add a New Tag:
Separated by commas
Ex.: root locus, bode
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.
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.