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:
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 12

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 12

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 12

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 12

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 12

> 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 12

> 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 12

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 12

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 12

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 12

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 12

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

Subject: Vectorizing Assignment to array of structures?

From: chendur kumaran

Date: 26 Dec, 2009 13:35:04

Message: 12 of 12

Here in the previous example
> data(1).mean = 5;
> data(2).mean = 7;
mean is a scalar number.
 c = num2cell([5 7]);
   [data.mean] = c{:}; will work

 But If mean is a array of dimension( 5,1) and I have matrix ADD of dimension(5,2) to be added to data(1).mean and data(2).mean how should
I Vectoroize the code



"First Last" <nospam@nospamplease.com> wrote in message <fc1m8v$b6r$1@fred.mathworks.com>...
> 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.
>

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