vectorize this structure call?

조회 수: 6 (최근 30일)
Tom
Tom 2012년 1월 13일
I have failed at trying to vectorize this. Would love some help!
Thanks!
for l = 1:length(tree.Steps.Step)
wavelength(l) = tree.Steps.Step(l).ATTRIBUTE.Scanning_parameter_value;
for m = 1:length(tree.Steps.Step(l).Far_field.Order)
order_num(m,l) = tree.Steps.Step(l).Far_field.Order(m).ATTRIBUTE.Order_number;
efficiency(m,l) = tree.Steps.Step(l).Far_field.Order(m).ATTRIBUTE.Efficiency;
end
end
  댓글 수: 1
Charlie
Charlie 2013년 4월 28일
Did you have any success in vectorizing the outside loop?
I am trying to do a similar operation. I am trying the equivalent of cell2mat(tree.Steps.Step) which seems to successfully vectorise these. I am confused why I can put a structure into a matrix though.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2012년 1월 13일
Uggh, but yes it can be done. The following code is partly tested.
for l = 1:length(tree.Steps.Step)
wavelength(l) = tree.Steps.Step(l).ATTRIBUTE.Scanning_parameter_value;
temp1 = cell2mat(struct2cell(tree.Steps.Step(l).Far_field.Order));
temp2 = cell2mat(struct2cell([temp1.ATTRIBUTE]));
order_num(:,l) = [temp2.Order_number];
efficiency(:,l) = [temp2.Efficiency];
end
Possibly there are more efficient ways to handle this using structfun().
  댓글 수: 2
Tom
Tom 2012년 1월 17일
Thank you! This does remove the inner loop. Any thoughts on removing the outer loop?
Walter Roberson
Walter Roberson 2012년 1월 17일
I am not sure at the moment that removing the outer loop is possible, except through techniques such as cellfun(). I have not worked through it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by