find max value with for loop

114 visualizaciones (últimos 30 días)
sarp karayegen
sarp karayegen el 19 de Abr. de 2020
Respondida: Vimal Rathod el 23 de Abr. de 2020
B=[ 6 7 8 9 10 ]
C=[8]
I want to have a for loop to compare each of the elements in B with the latest maximum number from the for loop to get the final maximum number in B.
  1 comentario
David Hill
David Hill el 19 de Abr. de 2020
What have you done? Is this homework? If not, why not just:
C=max(B);

Iniciar sesión para comentar.

Respuestas (1)

Vimal Rathod
Vimal Rathod el 23 de Abr. de 2020
Hi,
You could use the following code snippet if you want to use for loops specifically to find max.
max = B(1);
for i = 2:numel(B)
if B(i) > max
max = B(i)
end
end
If you want to change something you could use this for loop but if your use case is to find max then I suggest using max function. Below is the link for max function.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by