필터 지우기
필터 지우기

How do I update my for loop iteration?

조회 수: 8 (최근 30일)
Darianne Ynez Sanchez
Darianne Ynez Sanchez 2022년 4월 29일
답변: Vatsal 2023년 9월 29일
I am currently working through code to find sections in data where the y-values are equal to zero for at least 60 seconds. I included my current code down below. So far I can get the for loop to work for one iteration, but nothing else. I want to update the value for serach_start after each iteration to search_start = i+j+1; and repeat the code. How can I do that? Thank you for your help in advance.
for i = search_start:acc_end
if sum(Y_axis(i:i+60)) == 0
trial_end(a) = i;
for j = 30:min([5*30,(acc_end - trial_end(a))])
if Y_axis(trial_end(a) + j) > 0
trial_start(a+1) = i+j -1;
search_start=i+j+1;
break
end
end
a = a+1;
break
end
end

답변 (1개)

Vatsal
Vatsal 2023년 9월 29일
Hi @Darianne Ynez Sanchez,
I understand that your code is working for the first iteration, and you want to update the “search_start” value dynamically. I am assuming that it is required to update “i”, which is the loop variable.
I am attaching the code below with my assumption that you want to update the loop variable “i” to “i+j+1” in the next iteration, and I am using a "while" loop instead of a "for" loop.
i = search_start;
flag = 0;
while i <= acc_end
if sum(Y_axis(i:i+60)) == 0
trial_end(a) = i;
j = 30;
while j <= min([5*30,(acc_end - trial_end(a))])
if Y_axis(trial_end(a) + j) > 0
trial_start(a+1) = i+j -1;
i = i+j+1;
flag = 1;
break
end
j = j + 1;
end
a = a+1;
if(flag == 0)
i = i + 1;
end
break
else
i = i + 1;
end
end
I hope this helps!

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by