How to find index of first 3 maximum number in matrix
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Moe
el 5 de Nov. de 2014
Respondida: Aida Arman
el 21 de Nov. de 2018
I have the following function that n is the first 3 maximum value of matrix m:
m = [7;1;4;4;12;2;6;10;2];
temp = sort( m, 'descend' );
n = temp(1:3)
I need to know the index of matrix n, like:
p = [5;8;1]; % first max value in matrix m is 12 that it located in the fifth row
0 comentarios
Respuesta aceptada
Orion
el 5 de Nov. de 2014
Editada: Orion
el 5 de Nov. de 2014
use the second output argument of sort
m = [7;1;4;4;12;2;6;10;2];
[temp,originalpos] = sort( m, 'descend' );
n = temp(1:3)
p=originalpos(1:3)
1 comentario
Amit Kumar
el 21 de Feb. de 2018
Editada: Amit Kumar
el 21 de Feb. de 2018
Great.. I just want to add a comment that if you have NaN in your matrix replace beforehand as m(isnan(m))=0 and then use, as:
m = [7;1;4;4;NaN;2;6;10;2];
m(isnan(m))=0
[temp,originalpos] = sort( m, 'descend' );
n = temp(1:3)
p=originalpos(1:3)
Más respuestas (2)
Ver también
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!