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:
struct prealocation

Subject: struct prealocation

From: Michal Kvasnicka

Date: 6 Nov, 2012 10:32:08

Message: 1 of 4

How can I prealocate structure to eliminate allocation overhead during computation?

Example:
s ... size 1x10 struct
s.x ... size 1x1 double
s.y ... size 1x1 double
s.z ... size 1x1 double

Thanks in advance, Michal

Subject: struct prealocation

From: Barry Williams

Date: 6 Nov, 2012 11:25:08

Message: 2 of 4

"Michal Kvasnicka" wrote in message <k7aov7$al6$1@newscl01ah.mathworks.com>...
> How can I prealocate structure to eliminate allocation overhead during computation?
>
> Example:
> s ... size 1x10 struct
> s.x ... size 1x1 double
> s.y ... size 1x1 double
> s.z ... size 1x1 double
>
> Thanks in advance, Michal

Use the function struct with or without repmat and cell arrays. The fields above that contain the double precision number can be preallocated using zeros(m,n).
Barry

Subject: struct prealocation

From: Michal Kvasnicka

Date: 6 Nov, 2012 11:32:06

Message: 3 of 4

"Michal Kvasnicka" wrote in message <k7aov7$al6$1@newscl01ah.mathworks.com>...
> How can I prealocate structure to eliminate allocation overhead during computation?
>
> Example:
> s ... size 1x10 struct
> s.x ... size 1x1 double
> s.y ... size 1x1 double
> s.z ... size 1x1 double
>
> Thanks in advance, Michal

Well, I use this structure initialization:
s(10).x = [];
s(10).y = [];
s(10),z = [];

with good performance.

Subject: struct prealocation

From: Barry Williams

Date: 6 Nov, 2012 18:19:09

Message: 4 of 4

"Michal Kvasnicka" wrote in message <k7asfm$l23$1@newscl01ah.mathworks.com>...
> "Michal Kvasnicka" wrote in message <k7aov7$al6$1@newscl01ah.mathworks.com>...
> > How can I prealocate structure to eliminate allocation overhead during computation?
> >
> > Example:
> > s ... size 1x10 struct
> > s.x ... size 1x1 double
> > s.y ... size 1x1 double
> > s.z ... size 1x1 double
> >
> > Thanks in advance, Michal
>
> Well, I use this structure initialization:
> s(10).x = [];
> s(10).y = [];
> s(10),z = [];
>
> with good performance.

So what's your question? When you grow the variables, do s(10).x, s(10).y and s(10).z remean scalars? If no, then you're not preallocating those variables in way that will speed up your program when it executes.
Barry

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