How do I update my for loop iteration?
조회 수: 8 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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!
댓글 수: 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!