Got Questions? Get Answers.
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:
Preallocating structures

Subject: Preallocating structures

From: Kian

Date: 17 Mar, 2009 20:02:54

Message: 1 of 20

I'm trying to preallocate structures, but I can't find any information on how to do this.

Here's what I have:

myStructure.Channels.Units.Trial

There should be 96 channels, 5 units, 100 trials, and then an array of spike times for each trial.

For example, myStructure.Channel(4).Unit(2).Trial(27)

should give me all the spike times that occurred on channel 4 by unit 2 and on the 27th trial.

I'd like to preallocate my structure so the code runs faster. I know how to preallocate a single simple structure:

myStructure(10) = struct('fieldname', []);

Any idea?

Subject: Preallocating structures

From: someone

Date: 17 Mar, 2009 20:12:02

Message: 2 of 20

"Kian " <kian.torab@utah.edu> wrote in message <gpovld$r76$1@fred.mathworks.com>...
> I'm trying to preallocate structures, but I can't find any information on how to do this.
>
> Here's what I have:
>
> myStructure.Channels.Units.Trial
>
> There should be 96 channels, 5 units, 100 trials, and then an array of spike times for each trial.
>
> For example, myStructure.Channel(4).Unit(2).Trial(27)
>
> should give me all the spike times that occurred on channel 4 by unit 2 and on the 27th trial.
>
> I'd like to preallocate my structure so the code runs faster. I know how to preallocate a single simple structure:
>
> myStructure(10) = struct('fieldname', []);
>
> Any idea?

% I believe if you simply use something like:

myStructure.Channel(4).Unit(2).Trial(27) = 0

% MATLAB will create and "preallocate" the myStructure array with all zeros.

Subject: Preallocating structures

From: someone

Date: 17 Mar, 2009 20:22:01

Message: 3 of 20

"Kian " <kian.torab@utah.edu> wrote in message <gpovld$r76$1@fred.mathworks.com>...
> I'm trying to preallocate structures, but I can't find any information on how to do this.
>
> Here's what I have:
>
> myStructure.Channels.Units.Trial
>
> There should be 96 channels, 5 units, 100 trials, and then an array of spike times for each trial.
>
> For example, myStructure.Channel(4).Unit(2).Trial(27)
>
> should give me all the spike times that occurred on channel 4 by unit 2 and on the 27th trial.
>
> I'd like to preallocate my structure so the code runs faster. I know how to preallocate a single simple structure:
>
> myStructure(10) = struct('fieldname', []);
>
> Any idea?

You might also be interested in this:

http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/

Subject: Preallocating structures

From: Kian

Date: 17 Mar, 2009 20:38:01

Message: 4 of 20

myStructure.Channel(96).Unit(6).Trial(50) = 0; did not work. It only created a myStructure with 96 channels, but each channel does not have 6 units and 50 trials in them.

I'm still trying to see how the link you posted may answer my question. I believe it has information on what I already knew.

Thanks for your time!

Subject: Preallocating structures

From: Bruno Luong

Date: 17 Mar, 2009 20:51:01

Message: 5 of 20

UC(1:5)={zeros(100,1)};
CC(1:96)={struct('Trial', UC)};
myStructure.Channel=struct('Unit',CC);

myStructure.Channel(96).Unit(5).Trial(98)=pi

% Bruno

Subject: Preallocating structures

From: us

Date: 17 Mar, 2009 20:54:01

Message: 6 of 20

"Kian"
> myStructure.Channels.Units.Trial
> There should be 96 channels, 5 units, 100 trials, and then an array of spike times for each trial.
> I'd like to preallocate my structure so the code runs faster...

one of the solutions
- note: the typical struct allocation scheme does NOT work for your

% the data
     iv=10; % <- initial value at each end-point
     sa={ % <- anatomy of the struct
          'c' [1,4] % <- fieldname size_of_field
          'b' [3,3]
          'a' [2,1]
     };
% the engine
     st=iv;
for i=1:size(sa,1)
     st=struct(sa{i,1},repmat(st,sa{i,2}));
end
% the result
     st.a(1).b(1).c
     st.a(end).b(end).c
%{
     ans = 10 10 10 10
     ans = 10 10 10 10
%}

us

Subject: Preallocating structures

From: Kian

Date: 17 Mar, 2009 21:10:03

Message: 7 of 20

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gpp2fl$eb6$1@fred.mathworks.com>...
> UC(1:5)={zeros(100,1)};
> CC(1:96)={struct('Trial', UC)};
> myStructure.Channel=struct('Unit',CC);
>
> myStructure.Channel(96).Unit(5).Trial(98)=pi
>
> % Bruno

That's it! Thanks Bruno!

Subject: Preallocating structures

From: TideMan

Date: 17 Mar, 2009 21:28:10

Message: 8 of 20

On Mar 18, 10:10=A0am, "Kian " <kian.to...@utah.edu> wrote:
> "Bruno Luong" <b.lu...@fogale.findmycountry> wrote in message <gpp2fl$eb.=
..@fred.mathworks.com>...
> > UC(1:5)=3D{zeros(100,1)};
> > CC(1:96)=3D{struct('Trial', UC)};
> > myStructure.Channel=3Dstruct('Unit',CC);
>
> > myStructure.Channel(96).Unit(5).Trial(98)=3Dpi
>
> > % Bruno
>
> That's it! Thanks Bruno!

But wait!!
Why do we need to preallocate structures at all?

My understanding is that arrays take consecutive storage locations in
RAM, therefore you need to preallocate them to grab the amount of
memory you need.

But structures and cells are different because they are not stored in
consecutive locations (i.e., they are fragmented). So, for example,
myStructure.Channel(96).Unit(5).Trial(98) will not necessarily be
alongside myStructure.Channel(96).Unit(5).Trial(97) in RAM.
Therefore, there is no need to preallocate it. Matlab will grab the
space it needs for each element of the structure from somewhere in
RAM.

Is my understanding correct?

Subject: Preallocating structures

From: us

Date: 17 Mar, 2009 21:36:01

Message: 9 of 20

TideMan
> But structures and cells are different because they are not stored in
> consecutive locations (i.e., they are fragmented). So, for example,
> myStructure.Channel(96).Unit(5).Trial(98) will not necessarily be
> alongside myStructure.Channel(96).Unit(5).Trial(97) in RAM.
> Therefore, there is no need to preallocate it. Matlab will grab the
> space it needs for each element of the structure from somewhere in
> RAM.
> Is my understanding correct...

derek
yes, but my assumption was that the OP wanted to make sure his/her structure will fit into memory when completely filled...
just a thought...
urs

Subject: Preallocating structures

From: Bruno Luong

Date: 17 Mar, 2009 21:40:18

Message: 10 of 20

TideMan <mulgor@gmail.com> wrote in message <43d7b890-073d-44cd-943b-f0a697aa53fd@d36g2000prf.googlegroups.com>...
> On Mar 18, 10:10=A0am, "Kian " <kian.to...@utah.edu> wrote:
> > "Bruno Luong" <b.lu...@fogale.findmycountry> wrote in message <gpp2fl$eb.=
> ..@fred.mathworks.com>...
> > > UC(1:5)=3D{zeros(100,1)};
> > > CC(1:96)=3D{struct('Trial', UC)};
> > > myStructure.Channel=3Dstruct('Unit',CC);
> >
> > > myStructure.Channel(96).Unit(5).Trial(98)=3Dpi
> >
> > > % Bruno
> >
> > That's it! Thanks Bruno!
>
> But wait!!
> Why do we need to preallocate structures at all?
>
> My understanding is that arrays take consecutive storage locations in
> RAM, therefore you need to preallocate them to grab the amount of
> memory you need.
>
> But structures and cells are different because they are not stored in
> consecutive locations (i.e., they are fragmented). So, for example,
> myStructure.Channel(96).Unit(5).Trial(98) will not necessarily be
> alongside myStructure.Channel(96).Unit(5).Trial(97) in RAM.
> Therefore, there is no need to preallocate it. Matlab will grab the
> space it needs for each element of the structure from somewhere in
> RAM.
>
> Is my understanding correct?

From my understanding, cell and struct are indeed array of pointers. When we allocate as here, there is a consecutive array of pointers that are allocating. That would do admitly only part of the work because the fields (at the deepest level) might not be allocated (Not the case in this example).

I might be wrong though.

Bruno

Subject: Preallocating structures

From: TideMan

Date: 17 Mar, 2009 22:00:18

Message: 11 of 20

On Mar 18, 10:36=A0am, "us " <u...@neurol.unizh.ch> wrote:
> TideMan
>
> > But structures and cells are different because they are not stored in
> > consecutive locations (i.e., they are fragmented). So, for example,
> > myStructure.Channel(96).Unit(5).Trial(98) will not necessarily be
> > alongside myStructure.Channel(96).Unit(5).Trial(97) in RAM.
> > Therefore, there is no need to preallocate it. =A0Matlab will grab the
> > space it needs for each element of the structure from somewhere in
> > RAM.
> > Is my understanding correct...
>
> derek
> yes, but my assumption was that the OP wanted to make sure his/her struct=
ure will fit into memory when completely filled...
> just a thought...
> urs

OK, urs, but he/she needs to appreciate the difference between
preallocating an array and preallocating a structure or cell. The
former will enhance the performance of Matlab, the latter will not.

Subject: Preallocating structures

From: Bruno Luong

Date: 17 Mar, 2009 22:09:01

Message: 12 of 20

Or if you prefer a condensed horrible single statement:

myStructure.Channel=struct('Unit', ...
    repmat({struct('Trial', ...
            repmat({zeros(100,1)},5,1))},96,1));

% Bruno

Subject: Preallocating structures

From: us

Date: 17 Mar, 2009 22:14:01

Message: 13 of 20

"Bruno Luong"
> Or if you prefer a condensed horrible single statement:
> myStructure.Channel=struct('Unit', ...
> repmat({struct('Trial', ...
> repmat({zeros(100,1)},5,1))},96,1));

(i guess) that's exactly what my solution does - only in a more versatile way...

us

Subject: Preallocating structures

From: Bruno Luong

Date: 17 Mar, 2009 22:33:02

Message: 14 of 20

TideMan <mulgor@gmail.com> wrote in message <2d618045-1f5b-496b-befb-10e3c8fe19bb@f1g2000prb.googlegroups.com>...
> On Mar 18, 10:36=A0am, "us " <u...@neurol.unizh.ch> wrote:
>
> OK, urs, but he/she needs to appreciate the difference between
> preallocating an array and preallocating a structure or cell. The
> former will enhance the performance of Matlab, the latter will not.

I'm not convinced:

m=500;
n=500;
p=1;
tic
UC(1:n)={zeros(p,1)};
CC(1:m)={struct('Trial', UC)};
myStructure.Channel=struct('Unit',CC);
for i=1:m
    for j=1:n
        for k=1:p
            myStructure.Channel(i).Unit(j).Trial(j)=1;
        end
    end
end
toc % 1.785147 seconds

clear myStructure UC CC

tic
for i=1:m
    for j=1:n
        for k=1:p
            myStructure.Channel(i).Unit(j).Trial(j)=1;
        end
    end
end
toc % 8.052421 seconds.

% Bruno

Subject: Preallocating structures

From: Bruno Luong

Date: 17 Mar, 2009 23:15:06

Message: 15 of 20

"us " <us@neurol.unizh.ch> wrote in message <gpp7b9$n5f$1@fred.mathworks.com>...

>
> (i guess) that's exactly what my solution does - only in a more versatile way...
>

Not quite *exactly* us. Your solution applies repmat on single struct. Mine use repmat on the field values to create multiple struct entities.

Bruno

Subject: Preallocating structures

From: Kian

Date: 18 Mar, 2009 01:10:04

Message: 16 of 20

Yes, I wanted to know how to preallocate for performance purposes. It does make a huge difference. Thanks everyone for your quick responses.

Subject: Preallocating structures

From: us

Date: 18 Mar, 2009 13:19:01

Message: 17 of 20

"Bruno Luong"
> "us "
> > (i guess) that's exactly what my solution does - only in a more versatile way...

> Not quite *exactly* us. Your solution applies repmat on single struct. Mine use repmat on the field values to create multiple struct entities...

you are quite correct bruno - in the strict(!) sense re syntax/flow...
what i meant to say is:
- both solutions use cascading REPMATs
- us's solution is (a bit) more versatile because of the recursion loop, which is based
  on an easily definable structure-anatomy
- us's solution is considerably faster for large(r) constructs (see below)

as ever, best with just a few thoughts...
urs

% timing of solutions
% the data
     nt=1000; % <- #runs
     s1=1000; % <- size fields 1:3
     s2=1000;
     s3=1000;
% sol bruno
tic;
for i=1:nt
     sbl.A=struct('B',...
          repmat({struct('C',...
          repmat({zeros(s1,1)},s2,1))},s3,1));
end
toc

% sol urs
tic;
for i=1:nt
     iv=0; % <- initial value at each end-point
     sa={ % <- anatomy of the struct
          'C' [s1,1] % <- fieldname size_of_field
          'B' [s2,1]
          'A' [s3,1]
     };
     sus=iv;
for ix=1:size(sa,1)
     tmp=repmat(sus,sa{ix,2});
     sus=struct(sa{ix,1},tmp);
end
end
toc
% the result
     disp(isequal(sbl,sus));
%{
% wintel system:
% ic2/2*2.6ghz/2gb/winxp.sp3/r2008b
Elapsed time is 0.730798 seconds. % <- bruno
Elapsed time is 0.227390 seconds. % <- urs
isequal = 1
%}

Subject: Preallocating structures

From: Siyi

Date: 19 Mar, 2009 05:34:16

Message: 18 of 20

This is a very informative thread.

Subject: Preallocating structures

From: Jonathan

Date: 13 Sep, 2011 17:52:26

Message: 19 of 20

Siyi <Mr.Siyi.Deng@gmail.com> wrote in message <437ce7d1-b870-4b4a-9ba7-6ca32cf562b0@v23g2000pro.googlegroups.com>...
> This is a very informative thread.

Maybe, except nobody seems to understand that the OP was asking about a structure of arrays, and not the other way around...

Subject: Preallocating structures

From: Martin

Date: 1 May, 2013 07:50:09

Message: 20 of 20

I'm reviving the thread because I have a huge script and any second I can shave off saves me hours..
I'm trying to preallocate a big structure, but I am not sure I can translate the above into my specific case; can I get some input?

What I have is a structure(12,4,9), with each structure(i,j,k) has several fileds, some with a single value, some with a vector; e.g.
structure(i,j,k).Dim1=5.54
structure(i,j,k).Dim2=[194x1 Double]
....

Now, I am wondering if I can Qipreallocate both the matrix of structures.

could I ask some help in wrting the correct preallocation for this matrix?
 Thank you

 Martin

Tags for 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