How do I update my for loop iteration?

7 次查看(过去 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 个)

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