How do I update my for loop iteration?

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 日

0 投票

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!

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2022 年 4 月 29 日

回答済み:

2023 年 9 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by