Vectorization of for/loop

1 Ansicht (letzte 30 Tage)
Dimitrios
Dimitrios am 18 Mai 2014
Bearbeitet: Matt J am 18 Mai 2014
I have the following code:
for ii = 1: length(angles)
laminate.ply(ii).angle = angles(ii)/180*pi;
laminate.ply(ii).t = thickness(ii);
laminate.ply(ii).material = mat_ex3_ge();
end
How to impliment it without for/loop?

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 18 Mai 2014
Bearbeitet: Andrei Bobrov am 18 Mai 2014
laminate.ply = struct('angles',num2cell(angles(:)/180*pi),...
't',num2cell(thickness(:)),...
'material',repmat({mat_ex3_ge()},numel(angles),1));
  3 Kommentare
Andrei Bobrov
Andrei Bobrov am 18 Mai 2014
Thank you, corrected.
Matt J
Matt J am 18 Mai 2014
Bearbeitet: Matt J am 18 Mai 2014
Note, however, that this is not truly vectorized. The num2cell function uses for-loops,
>>type num2cell

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 18 Mai 2014
There's no way without a for-loop or equivalent.
But regardless, it's a bad way to organize your data. It splits the angles(ii), thickness(ii), etc... data in an inefficient way. Store all data in one field instead,
laminate.ply.angles=angles*180/pi;
laminate.ply.t=thickness;
laminate.ply.material(1:length(angles))=mat_ex3_ge();

Kategorien

Mehr zu Audio I/O and Waveform Generation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by