How to print the second largest of a group of the numbers

1 visualización (últimos 30 días)
Rodrigo Franco
Rodrigo Franco el 6 de Oct. de 2014
Editada: Guillaume el 6 de Oct. de 2014
Example: number : 1 2 3 4 5 6 7 8 9 10
second largest = 9

Respuestas (2)

Mischa Kim
Mischa Kim el 6 de Oct. de 2014
Rodrigo, you could use
a = [1 2 3 4 5 6 7 8 9 10];
b = max(setxor(a,max(a)));

Guillaume
Guillaume el 6 de Oct. de 2014
Editada: Guillaume el 6 de Oct. de 2014
Another option (which also allows you to pick any nth largest number):
a = [1 2 3 4 5 6 7 8 9 10];
ua = unique(a); %will sort a and remove duplicate
b = ua(end-1); %for the 2nd largest
b = ua(end-n+1); %for the nth largest

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by