Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: How to avoid for loop to assign values to fields in structure
Replies: 1   Last Post: Aug 28, 2013 10:11 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Marco

Posts: 4
Registered: 8/7/13
How to avoid for loop to assign values to fields in structure
Posted: Aug 27, 2013 4:06 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi,

i am trying to avoid using a for loop as it is not efficient for the particular part of my code.

i have a structure say:
a.a.a=[];
a.a.b=[];
a.a.c=[];
a.a.d=[];

I have the variables:
var1=rand(84,7);
var2=zeros(84,1);
var2([15,25,84])=1;
value=21;

names=fieldnames(a.a);
for m=1:size(names,1)
a.a.(genvarname(names{m}))=[var1((value*(m-1)+1):value*m,6),...
var1((value*(m-1)+1):value*m,7),...
var2((value*(m-1)+1):value*m)];
a.a.(genvarname(names{m}))(a.a.(genvarname(names{m}))(:,3)==0,:)=[];
end

There must be a more efficient way of writing this.

Thank you very much for your help.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.