assign vector accross a vector of structures

  • Follow


Lets say I have a vector struct
a(1).x = 1
a(1).y = 'a'
a(2).x = 2
a(2).y = 'b'

v = [10, 11] 

what i want to do is:
a.x = v 

how can this be done?
0
Reply Peter 1/6/2011 3:46:20 PM

"Peter" wrote in message <ig4o4c$1jh$1@fred.mathworks.com>...
>
> what i want to do is:
> a.x = v 
> 

Which means what? Do you mean a(i).x=v or instead a(i).x=v(i) for all i?

The former:

[a.x]=deal(v);

The latter:

v=num2cell(v);
[a.x]=deal(v{:});

The latter, splitting vector data across structure array fields, is generally a dubious thing to do, however. It makes data access much slower.
0
Reply Matt 1/6/2011 4:00:09 PM


I meant: 
a(i).x=v(i) for all i

why is it a dubious thing to do? 
0
Reply Peter 1/6/2011 4:10:18 PM

"Peter" wrote in message <ig4pha$5hf$1@fred.mathworks.com>...
> I meant: 
> a(i).x=v(i) for all i
> 
> why is it a dubious thing to do? 

Well, the data v(i) is stored contiguously in memory whereas the data data a(i).x is not.
That means that the data as it resides in v can be accessed/manipulated much faster. This probably wouldn't be a big deal unless length(v) is really large...
0
Reply Matt 1/6/2011 4:33:04 PM

"Matt J" wrote in message <ig4qs0$5v1$1@fred.mathworks.com>...
> "Peter" wrote in message <ig4pha$5hf$1@fred.mathworks.com>...
> > I meant: 
> > a(i).x=v(i) for all i
> > 
> > why is it a dubious thing to do? 
> 
> Well, the data v(i) is stored contiguously in memory whereas the data data a(i).x is not.
> That means that the data as it resides in v can be accessed/manipulated much faster. This probably wouldn't be a big deal unless length(v) is really large...

Interesting. Thanks a lot
0
Reply Peter 1/7/2011 7:12:05 AM

4 Replies
265 Views

(page loaded in 0.06 seconds)

Similiar Articles:













7/13/2012 10:51:47 PM


Reply: