Thread Subject: Vectorizing Assignment to array of structures?

Subject: Vectorizing Assignment to array of structures?

From: First Last

Date: 9 Sep, 2007 20:49:35

Message: 1 of 11

Hello,

I have a variable 'data' that is a 1x2 structure array with
the fields in which:

data.times are 5x1 vectors
data.mean are numbers

I can clearly access the data.mean and data.times values and
manipulate them, but is there a way I can assign each one of
the data.mean values in a vector, quickly, without a loop?

For instance, let's say I want

data(1).mean = 5;
data(2).mean = 7;

Is there a way to assign the vector [5 7] to data.mean?
Obviously this example there is only a marginal
computational penalty for doing this somewhat manually, but
for larger arrays of structures, this could be a nightmare.
I'm trying to vectorize this thing as much as possible.

Thanks in advance.

Subject: Vectorizing Assignment to array of structures?

From: david bateman

Date: 9 Sep, 2007 21:33:54

Message: 2 of 11

First Last wrote:
> Hello,
>
> I have a variable 'data' that is a 1x2 structure array with
> the fields in which:
>
> data.times are 5x1 vectors
> data.mean are numbers
>
> I can clearly access the data.mean and data.times values and
> manipulate them, but is there a way I can assign each one of
> the data.mean values in a vector, quickly, without a loop?
>
> For instance, let's say I want
>
> data(1).mean = 5;
> data(2).mean = 7;
>
> Is there a way to assign the vector [5 7] to data.mean?
> Obviously this example there is only a marginal
> computational penalty for doing this somewhat manually, but
> for larger arrays of structures, this could be a nightmare.
> I'm trying to vectorize this thing as much as possible.
>
> Thanks in advance.
>

Maybe I'm missing something, but what is wrong with

data = struct('mean', {5,7})


D.

Subject: Vectorizing Assignment to array of structures?

From: Doug Schwarz

Date: 9 Sep, 2007 21:35:18

Message: 3 of 11

First Last wrote:
> Hello,
>
> I have a variable 'data' that is a 1x2 structure array with
> the fields in which:
>
> data.times are 5x1 vectors
> data.mean are numbers
>
> I can clearly access the data.mean and data.times values and
> manipulate them, but is there a way I can assign each one of
> the data.mean values in a vector, quickly, without a loop?
>
> For instance, let's say I want
>
> data(1).mean = 5;
> data(2).mean = 7;
>
> Is there a way to assign the vector [5 7] to data.mean?

If you are creating data from scratch then use

   data = struct('mean',num2cell([5 7]));

If data already exists and is the right size then use

   c = num2cell([5 7]);
   [data.mean] = c{:};

or if you are using a version of MATLAB before 7:

   c = num2cell([5 7]);
   [data.mean] = deal(c{:});


--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Vectorizing Assignment to array of structures?

From: First Last

Date: 9 Sep, 2007 22:03:44

Message: 4 of 11

Doug Schwarz <see@sig.for.address.edu> wrote in message <

>
> If data already exists and is the right size then use
>
> c = num2cell([5 7]);
> [data.mean] = c{:};
>
> or if you are using a version of MATLAB before 7:
>
> c = num2cell([5 7]);
> [data.mean] = deal(c{:});
>

Ah, thanks Doug (and ez). That did it.

Subject: Vectorizing Assignment to array of structures?

From: Lee Newman

Date: 9 Nov, 2007 20:05:45

Message: 5 of 11

> If data already exists and is the right size then use
>
> c = num2cell([5 7]);
> [data.mean] = c{:};

> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.


Is there a way to do this without the intermediate
assignment to c? [data.mean]= (num2cell(stuff)){:} would be
great, but Matlab doesn't allow this...

Subject: Vectorizing Assignment to array of structures?

From: Lee Newman

Date: 9 Nov, 2007 20:06:21

Message: 6 of 11

> If data already exists and is the right size then use
>
> c = num2cell([5 7]);
> [data.mean] = c{:};

> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.


Is there a way to do this without the intermediate
assignment to c? [data.mean]= (num2cell(stuff)){:} would be
great, but Matlab doesn't allow this...

Subject: Vectorizing Assignment to array of structures?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 9 Nov, 2007 20:35:26

Message: 7 of 11

In article <fh2ejt$nsv$1@fred.mathworks.com>,
Lee Newman <newman.lee.nospam@comcast.net> wrote:
>> If data already exists and is the right size then use

>> c = num2cell([5 7]);
>> [data.mean] = c{:};

>Is there a way to do this without the intermediate
>assignment to c? [data.mean]= (num2cell(stuff)){:} would be
>great, but Matlab doesn't allow this...

unfold = @(v) v{:};
[data.mean] = unfold(num2cell([5 7]));
--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Subject: Vectorizing Assignment to array of structures?

From: Doug Schwarz

Date: 9 Nov, 2007 21:26:33

Message: 8 of 11

In article <fh2eip$n96$1@fred.mathworks.com>,
 "Lee Newman" <newman.lee.nospam@comcast.net> wrote:

> > If data already exists and is the right size then use
> >
> > c = num2cell([5 7]);
> > [data.mean] = c{:};
>
> > Doug Schwarz
> > dmschwarz&ieee,org
> > Make obvious changes to get real email address.
>
>
> Is there a way to do this without the intermediate
> assignment to c? [data.mean]= (num2cell(stuff)){:} would be
> great, but Matlab doesn't allow this...

Well, if data doesn't already exist you can do

  data = struct('mean',num2cell([5 7]));

Walter's method is fine if you can reuse unfold, but if you only have to
do it once I don't see an advantage.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Vectorizing Assignment to array of structures?

From: per isakson

Date: 10 Nov, 2007 15:18:30

Message: 9 of 11

David Bateman <dbateman@free.fr> wrote in message <46e466c2
$0$7346$426a74cc@news.free.fr>...
> First Last wrote:
> > Hello,
> >
> > I have a variable 'data' that is a 1x2 structure array
with
> > the fields in which:
> >
> > data.times are 5x1 vectors
> > data.mean are numbers
> >
> > I can clearly access the data.mean and data.times
values and
> > manipulate them, but is there a way I can assign each
one of
> > the data.mean values in a vector, quickly, without a
loop?
> >
> > For instance, let's say I want
> >
> > data(1).mean = 5;
> > data(2).mean = 7;
> >
> > Is there a way to assign the vector [5 7] to data.mean?
> > Obviously this example there is only a marginal
> > computational penalty for doing this somewhat manually,
but
> > for larger arrays of structures, this could be a
nightmare.
> > I'm trying to vectorize this thing as much as possible.
> >
> > Thanks in advance.
> >
>
> Maybe I'm missing something, but what is wrong with
>
> data = struct('mean', {5,7})
>
>
> D.

Nothing is wrong as far as I can tell - with R2007a

>> data = struct('mean', {5,7})
data =
1x2 struct array with fields:
    mean
>> data
data =
1x2 struct array with fields:
    mean
>> data.mean
ans =
     5
ans =
     7

/ per

Subject: Vectorizing Assignment to array of structures?

From: Doug Schwarz

Date: 10 Nov, 2007 15:34:37

Message: 10 of 11

In article <fh4i46$5i8$1@fred.mathworks.com>,
 "per isakson" <poi.nospam@bimDOTkthDOT.se> wrote:

> David Bateman <dbateman@free.fr> wrote in message <46e466c2
> $0$7346$426a74cc@news.free.fr>...
> > First Last wrote:
> > > Hello,
> > >
> > > I have a variable 'data' that is a 1x2 structure array
> with
> > > the fields in which:
> > >
> > > data.times are 5x1 vectors
> > > data.mean are numbers
> > >
> > > I can clearly access the data.mean and data.times
> values and
> > > manipulate them, but is there a way I can assign each
> one of
> > > the data.mean values in a vector, quickly, without a
> loop?
> > >
> > > For instance, let's say I want
> > >
> > > data(1).mean = 5;
> > > data(2).mean = 7;
> > >
> > > Is there a way to assign the vector [5 7] to data.mean?
> > > Obviously this example there is only a marginal
> > > computational penalty for doing this somewhat manually,
> but
> > > for larger arrays of structures, this could be a
> nightmare.
> > > I'm trying to vectorize this thing as much as possible.
> > >
> > > Thanks in advance.
> > >
> >
> > Maybe I'm missing something, but what is wrong with
> >
> > data = struct('mean', {5,7})
> >
> >
> > D.
>
> Nothing is wrong as far as I can tell - with R2007a
>
> >> data = struct('mean', {5,7})
> data =
> 1x2 struct array with fields:
> mean
> >> data
> data =
> 1x2 struct array with fields:
> mean
> >> data.mean
> ans =
> 5
> ans =
> 7
>
> / per

The OP wanted to fill an existing field (mean) in a structure array with
numbers from a vector. The statement above will create a new structure,
data, with only one field. We don't know what the larger problem is and
whether there might be a better way to go about it.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Vectorizing Assignment to array of structures?

From: per isakson

Date: 10 Nov, 2007 15:57:24

Message: 11 of 11

Doug Schwarz <see@sig.for.address.edu> wrote in message
<see-904A4E.10343610112007@71-129-133-66.dollamir.com>...
> In article <fh4i46$5i8$1@fred.mathworks.com>,
> "per isakson" <poi.nospam@bimDOTkthDOT.se> wrote:
>
> > David Bateman <dbateman@free.fr> wrote in message
<46e466c2
> > $0$7346$426a74cc@news.free.fr>...
> > > First Last wrote:
> > > > Hello,
> > > >
> > > > I have a variable 'data' that is a 1x2 structure
array
> > with
> > > > the fields in which:
> > > >
> > > > data.times are 5x1 vectors
> > > > data.mean are numbers
> > > >
> > > > I can clearly access the data.mean and data.times
> > values and
> > > > manipulate them, but is there a way I can assign
each
> > one of
> > > > the data.mean values in a vector, quickly, without
a
> > loop?
> > > >
> > > > For instance, let's say I want
> > > >
> > > > data(1).mean = 5;
> > > > data(2).mean = 7;
> > > >
> > > > Is there a way to assign the vector [5 7] to
data.mean?
> > > > Obviously this example there is only a marginal
> > > > computational penalty for doing this somewhat
manually,
> > but
> > > > for larger arrays of structures, this could be a
> > nightmare.
> > > > I'm trying to vectorize this thing as much as
possible.
> > > >
> > > > Thanks in advance.
> > > >
> > >
> > > Maybe I'm missing something, but what is wrong with
> > >
> > > data = struct('mean', {5,7})
> > >
> > >
> > > D.
> >
> > Nothing is wrong as far as I can tell - with R2007a
> >
> > >> data = struct('mean', {5,7})
> > data =
> > 1x2 struct array with fields:
> > mean
> > >> data
> > data =
> > 1x2 struct array with fields:
> > mean
> > >> data.mean
> > ans =
> > 5
> > ans =
> > 7
> >
> > / per
>
> The OP wanted to fill an existing field (mean) in a
structure array with
> numbers from a vector. The statement above will create a
new structure,
> data, with only one field. We don't know what the larger
problem is and
> whether there might be a better way to go about it.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Right - I missed the real question. / per

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
array of struct... First Last 9 Sep, 2007 16:50:22
vectorization First Last 9 Sep, 2007 16:50:22
rssFeed for this Thread
 

MATLAB Central Terms of Use

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 Terms prior to use.

Contact us at files@mathworks.com