how to use multiple 'for loop' for one function?
8 ビュー (過去 30 日間)
古いコメントを表示
intersection_point = [];
for i = 1:length(mean_trajectory_double)-1
for j = 1:length(RKSI_Arr_33R)
for k = 1:length(RKSI_Arr_33R.Latitude)
[I,check]=plane_line_intersect([mean_trajectory_double(i+1,1) mean_trajectory_double(i+1,2) NormalVectorsmean_trajectory_double(i+1,3)]...
,[mean_trajectory_double(i+1,1) mean_trajectory_double(i+1,2) mean_trajectory_double(i+1,3)]...
,[RKSI_Arr_33R(j).Longitude(k) RKSI_Arr_33R(j).Latitude(k) RKSI_Arr_33R(j).BAlt(k)]...
,[RKSI_Arr_33R(j).Longitude(k+1) RKSI_Arr_33R(j).Latitude(k+1) RKSI_Arr_33R(j).BAlt(k+1)]);
intersection_point = [intersection_point; ];
end
end
end
%% [I,check]=plane_line_intersect(n,V0,P0,P1)
I need to use multiple for loop for one function. I have used multiple for loop, but i have not used multiple for loop for one function.
this function need 4 input and 2 output(I,check). I is intersection_point and check is an indicator.
for more information about this function, https://kr.mathworks.com/matlabcentral/fileexchange/17751-straight-line-and-plane-intersection
What i want to do is that
when i is 1, j is 1 and k(1~100) is finished, it goes to i(1), j(2),k(1~100).
and j(1-200) is finisiehd, it goes to i(2),j(1),k(1~100) and i(2),j(2),k(1~100) like this.
if you need more clarification to answer my question, just tell me
Thanks.
7 件のコメント
Jan
2022 年 6 月 24 日
Kenneth means a "break point". Click in the bar on the left side in the editor. Then a red dot appears. Matlab stops at this point and you can check the varaibels used in this line.
The break command is something else.
Kenneth Joseph Paul
2022 年 6 月 24 日
You need to use a "break point". As you said a "break" teminates the loop. A "break point" is used to temporarily pause the execution of the code. You can follow the page linked below, if you are not familiar with "breakpoint"
採用された回答
Jan
2022 年 6 月 24 日
編集済み: Jan
2022 年 6 月 24 日
Please post the complete error message, if you mention an error. This message contains the information, in which line the problem occurs. I guess, it is here:
for i = 1:length(mean_trajectory_double)-1
for j = 1:length(RKSI_Arr_33R)
% RKSI_Arr_33R is a struct array
for k = 1:length(RKSI_Arr_33R.Latitude)
% Then RKSI_Arr_33R.Latitude is a list of arrays
The innermost FOR loop should look like:
for k = 1:length(RKSI_Arr_33R(j).Latitude)
% ^^^
What is the purpose of this line:
intersection_point = [intersection_point; ];
This concatenates the array intersection_point with nothing. Maybe you want:
intersection_point = [intersection_point; I];
The readability of your code would profit fromusing much simpler names for the variables.
R = RKSI_Arr_33R;
M = mean_trajectory_double;
N = NormalVectorsmean_trajectory_double;
Intersection = [];
for i = 1:numel(M)-1
for j = 1:length(R)
Lat = R(j).Latitude;
Long = R(j).Longitude;
BAlt = R(j).BAlt;
for k = 1:numel(Lat)
[I,check] = plane_line_intersect([M(i+1, 1:2), N(i+1,3)], ...
M(i+1, 1:3), ...
[Long(k), Lat(k), BAlt(k)], ...
[Long(k+1), Lat(k+1), BAlt(k+1)]);
intersection_point = [intersection_point; I];
end
end
end
In clear code, typos are much more obvious. In addition accessing a field in a struct costs time, so the simplified code runs even faster.
2 件のコメント
Jan
2022 年 6 月 24 日
編集済み: Jan
2022 年 6 月 24 日
"and where the function code shoul be located?" - what does this mean? I've mentioned, where I assume the error and how to fix it. Then I've posted a cleaned version of the complete code you have posted.
Concerning the original question: If the code does not containbugs, it is not problem to use nested loops inside one function.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!