MATLAB Answers

extract out values out of loop

조회 수: 16(최근 30일)
Oday Shahadh
Oday Shahadh 22 Dec 2020
편집: Jan 22 Dec 2020
Can any one pls help how to extarct [XYZ,H,D,I,F] out of the below loop
Thanks
for i= 1: length(Hi)
[XYZ,H,D,I,F] = wrldmagm(Hi(i),Lat(i),Long(i),decimalYear(i));
end
Can

  댓글 수: 1

dpb
dpb 22 Dec 2020
Preallocate and index, maybe?

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

채택된 답변

James Tursa
James Tursa 22 Dec 2020
Make them arrays and assign to the elements. E.g.,
for i= 1: length(Hi)
[XYZ(:,i),H(i),D(i),I(i),F(i)] = wrldmagm(Hi(i),Lat(i),Long(i),decimalYear(i));
end
Consider pre-allocating XYZ, H, D, I, and F also.

  댓글 수: 0

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

추가 답변(1개)

Jan
Jan 22 Dec 2020
편집: Jan 22 Dec 2020
% Pre-allocation of the output:
len = length(Hi);
XYZ = zeros(3, len);
H = zeros(1, len);
D = zeros(1, len);
I = zeros(1, len);
F = zeros(1, len);
for i = 1:length(Hi)
[XYZ(:, i), H(i), D(i), I(i), F(i)] = wrldmagm(Hi(i), Lat(i), Long(i), decimalYear(i));
end

  댓글 수: 0

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by