How can I find X smallest numbers in a Matrix?

I have a 480 by 640 matrix, and I would like to find 4 smallest number ( or closest values to zero).

 Respuesta aceptada

Matt J
Matt J el 2 de Jul. de 2013
Xs=sort(X(:));
FourSmallest = Xs(1:4);

7 comentarios

Matt J
Matt J el 2 de Jul. de 2013
Editada: Matt J el 2 de Jul. de 2013
There are also more optimized routines on the FEX, e.g.,
Laila Kazemi
Laila Kazemi el 2 de Jul. de 2013
will I also be able to obtain indices of the values in this method?
Matt J
Matt J el 2 de Jul. de 2013
Of course!
Laila Kazemi
Laila Kazemi el 2 de Jul. de 2013
How?
when I try:
[Xs Xind]=sort(X(:));
in Xind it return the indices as one number
You may have more than 4 pixels having the 4 smallest values. You can use ind2sub() or find
[rows1, columns1] = find(yourImage == Xs(1));
[rows2, columns2] = find(yourImage == Xs(2));
[rows3, columns3] = find(yourImage == Xs(3));
[rows4, columns4] = find(yourImage == Xs(4));
Matt J
Matt J el 2 de Jul. de 2013
Editada: Matt J el 2 de Jul. de 2013
in Xind it return the indices as one number
In case it matters, note that the "one number" will work as an index, even when used with the original 480x640 matrix, e.g.,
>> X=reshape((1:20)*10,4,5).'
X =
10 20 30 40
50 60 70 80
90 100 110 120
130 140 150 160
170 180 190 200
>> [Xs, Xind]=sort(X(:));
>> X(Xind(4))
ans =
40

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 2 de Jul. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by