how to use multiple 'for loop' for one function?

8 views (last 30 days)
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.
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 Comments
Jan
Jan on 24 Jun 2022
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
Kenneth Joseph Paul on 24 Jun 2022
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"

Sign in to comment.

Accepted Answer

Jan
Jan on 24 Jun 2022
Edited: Jan on 24 Jun 2022
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 Comments
Sierra
Sierra on 24 Jun 2022
the error message is in Korean. so I wrote like that.
and where the function code shoul be located? between for i = 1:length and for j=1:length or for k=1:length and end ?
Thanks Jan.
Jan
Jan on 24 Jun 2022
Edited: Jan on 24 Jun 2022
"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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!