while loop performance improvement

4 visualizaciones (últimos 30 días)
cglr
cglr el 17 de Nov. de 2019
Editada: cglr el 18 de Nov. de 2019
Hello everyone,
What is the alternative of while loop for the example:
rate = 2;
while (k <= period)
sum = sum + array(k,2);
k = k + rate;
end
Thanks.

Respuesta aceptada

Fabio Freschi
Fabio Freschi el 17 de Nov. de 2019
I don't know the starting value of k, assuming it is 1
idx = 1:rate:period
mysum = sum(array(idx,2)); % note that sum is the name of a matlab function
  1 comentario
cglr
cglr el 18 de Nov. de 2019
Editada: cglr el 18 de Nov. de 2019
Thank you.
maxDuration = 0;
while (k <= period)
if( myArray (k,2) > maxDuration)
maxDuration = myArray (k,2);
end
k = k + rate;
end
If I make it more complex with if statement like that, how can I use that idea ?

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by