how to select the second maximum value for an array.

121 visualizaciones (últimos 30 días)
jaah navi
jaah navi el 7 de En. de 2020
Comentada: Stephen23 el 7 de En. de 2020
A=[20 60 80 100 75 90 35 55]
with respect to A if i use the command max(A) it gives 100.
But i need to choose the second maximum value 90.
Could anyone please help me on this.

Respuestas (2)

ES
ES el 7 de En. de 2020
B = sort(A, 'descend');
Max2 = B(2);

Lucademicus
Lucademicus el 7 de En. de 2020
One way would be to sort the array and take the value of the second to last value.
[a,b] = sort(A);
maximum = A(b(end-1));
There are multiple ways to achieve this. I'm sure somebody will come up with a nicer oneliner, but this should do your job.

Categorías

Más información sobre Matrices and Arrays 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