Azzera filtri
Azzera filtri

How do I update my for loop iteration?

9 visualizzazioni (ultimi 30 giorni)
Darianne Ynez Sanchez
Darianne Ynez Sanchez il 29 Apr 2022
Risposto: Vatsal il 29 Set 2023
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

Risposte (1)

Vatsal
Vatsal il 29 Set 2023
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!

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by