"Continue" in "for loop" based on trial count

I have the following loop set up:
X_vector = [3; -3; -9; -15; -21; -15; -9; -3; 3; 3; -3; -9; -15; -21; -21; -15; -9; -3; 3; 3; -3; -9; -15; -21; -15; -9; -3; 3;];
Y_vector = [15; 15; 15; 15; 9; 9; 9; 9; 9; 3; 3; 3; 3; 3; -3; -3; -3; -3; -3; -9; -9; -9; -9; -9; -15; -15; -15; -15;];
for k = 1 : numel(X_vector)
x = X_vector(k);
y = Y_vector(k);
% Now use x and y in some way.
end
I want the loop to run through all the X and Y vector locations but change every 5 trials to the next X_vector and Y_vector pair (e.g. from (3, 15) to (-3, 15). Do I use a continue function to this?
Thank you in advance for your help!

 채택된 답변

Voss
Voss 2023년 3월 6일

0 개 추천

Maybe this:
for k = 1 : numel(X_vector)
for n = 1 : 5
x = X_vector(k);
y = Y_vector(k);
% Now use x and y in some way.
end
end

댓글 수: 6

HumbleCoder
HumbleCoder 2023년 3월 6일
Still not shifting unfortunately.
I interpreted the question to mean that you want to use the first element of X_vector and Y_vector 5 times, then go to the second element and use those 5 times, and so on, so that x and y would go like:
(3,15)
(3,15)
(3,15)
(3,15)
(3,15)
(-3,15)
(-3,15)
(-3,15)
(-3,15)
(-3,15)
(-9,15)
(-9,15)
(-9,15)
(-9,15)
(-9,15)
(-15,15)
(-15,15)
(-15,15)
(-15,15)
(-15,15)
etc.
If that's not what you want, then please explain what you do want.
HumbleCoder
HumbleCoder 2023년 3월 6일
Yep! Exactly what i want! When I tried your suggestion, it only ran 5 trials at the first paired X,Y coordinates then stopped.
Here's the same code as in my answer but with a disp statement to show (x,y) each time. You can see it's as intended.
X_vector = [3; -3; -9; -15; -21; -15; -9; -3; 3; 3; -3; -9; -15; -21; -21; -15; -9; -3; 3; 3; -3; -9; -15; -21; -15; -9; -3; 3;];
Y_vector = [15; 15; 15; 15; 9; 9; 9; 9; 9; 3; 3; 3; 3; 3; -3; -3; -3; -3; -3; -9; -9; -9; -9; -9; -15; -15; -15; -15;];
for k = 1 : numel(X_vector)
for n = 1 : 5
x = X_vector(k);
y = Y_vector(k);
disp([x,y]);
% Now use x and y in some way.
end
end
3 15 3 15 3 15 3 15 3 15 -3 15 -3 15 -3 15 -3 15 -3 15 -9 15 -9 15 -9 15 -9 15 -9 15 -15 15 -15 15 -15 15 -15 15 -15 15 -21 9 -21 9 -21 9 -21 9 -21 9 -15 9 -15 9 -15 9 -15 9 -15 9 -9 9 -9 9 -9 9 -9 9 -9 9 -3 9 -3 9 -3 9 -3 9 -3 9 3 9 3 9 3 9 3 9 3 9 3 3 3 3 3 3 3 3 3 3 -3 3 -3 3 -3 3 -3 3 -3 3 -9 3 -9 3 -9 3 -9 3 -9 3 -15 3 -15 3 -15 3 -15 3 -15 3 -21 3 -21 3 -21 3 -21 3 -21 3 -21 -3 -21 -3 -21 -3 -21 -3 -21 -3 -15 -3 -15 -3 -15 -3 -15 -3 -15 -3 -9 -3 -9 -3 -9 -3 -9 -3 -9 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 3 -3 3 -3 3 -3 3 -3 3 -3 3 -9 3 -9 3 -9 3 -9 3 -9 -3 -9 -3 -9 -3 -9 -3 -9 -3 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -15 -9 -15 -9 -15 -9 -15 -9 -15 -9 -21 -9 -21 -9 -21 -9 -21 -9 -21 -9 -15 -15 -15 -15 -15 -15 -15 -15 -15 -15 -9 -15 -9 -15 -9 -15 -9 -15 -9 -15 -3 -15 -3 -15 -3 -15 -3 -15 -3 -15 3 -15 3 -15 3 -15 3 -15 3 -15
Maybe something in the code you're running (which is replaced by "% Now use x and y in some way." here) runs into an error, which causes execution to stop, or exits the loops early.
HumbleCoder
HumbleCoder 2023년 3월 9일
The error was on my end!! Thank you so much for helping me!! Your suggestion works like a charm!
Voss
Voss 2023년 3월 9일
You're welcome! Glad you got it to work!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2023년 3월 6일

댓글:

2023년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by