How to make a loop end when you reach the end of some data

1 visualización (últimos 30 días)
AluminiumMan
AluminiumMan el 5 de Mzo. de 2024
Editada: Voss el 6 de Mzo. de 2024
I wish to implement a counter in MATLAB. The loop will increase a counter evertime some conditions are met.
I wish for the loop to cycle through each sample point in the data and then end when the loop has cycled through all data points in the variable.
How would I implement this?

Respuestas (1)

Voss
Voss el 5 de Mzo. de 2024
counter = 0;
for ii = 1:numel(data)
if condition
counter = counter+1;
end
end
  2 comentarios
AluminiumMan
AluminiumMan el 6 de Mzo. de 2024
Thank you. What does the ii do? Is that a variable I need to declare?
Also I only want the counter to go up once each time the data reaches 0, and then I dont want the counter to be able to increase again until the data goes above 100 and then drops to zero again
Voss
Voss el 6 de Mzo. de 2024
Editada: Voss el 6 de Mzo. de 2024
You're welcome!
"What does the ii do?"
ii is the index variable of the for-loop.
"Is that a variable I need to declare?"
It is defined in the for statement. That's all you need to do to declare ii.
"Also I only want the counter to go up once each time the data reaches 0, and then I dont want the counter to be able to increase again until the data goes above 100 and then drops to zero again"
Maybe something like this then:
counter = 0;
data_hit_100 = false;
for ii = 1:numel(data)
if data(ii) >= 100
data_hit_100 = true;
end
if data_hit_100 && data(ii) <= 0
counter = counter+1;
data_hit_100 = false;
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by