How to find least 8 values in a 1xN row matrix with their positions?

I have a row matrix with 94 elements, Ex: A=rand(1,94). Now i would like to find the least 8 values in the matrix 'A' with their positions & I would like to form a matrix with the 'position numbers' of least 8 values in matrix 'A'.
I used "[value,position]=min(A)", by using this i could find one least value and its position stored in value and position respectively. But i want 8 least values and their respective positions. Finally, i would like to form a row matrix (1 X 8) with position numbers of least 8 values of matrix 'A'

2 comentarios

Guillaume
Guillaume el 20 de Oct. de 2015
Editada: Guillaume el 20 de Oct. de 2015
Is it the least 8 different values, or just the least 8 regardless of if they're equal or not?
least 8 different values will be fine. least 8 regardless if they're equal or not will also be helpful. thank u

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 20 de Oct. de 2015
Editada: Guillaume el 20 de Oct. de 2015
If it's just least 8 regardless regardless of if they're equal or not, simply use sort and only keep the first 8 results:
A = rand(1, 94);
[Asorted, position] = sort(A);
least8 = Asorted(1:8);
position8 = position(1:8);
If it's the least 8 different values:
A = randi(20, 1, 94);
[Asorted, position] = sort(A);
diffvals = find([1 diff(Asorted)]);
least8 = Asorted(diffvals(1:8));
position8 = position(diffvals(1:8));

4 comentarios

Thank u, but if i use sort command my actual position value of the least numbers will be changed from 1 to 8 numbers. i want the exact position numbers without sorting.
position8 gives you the position in the original, unsorted array, same as ind in my answer below. If this is not what you want then please give an example.
Guillaume
Guillaume el 20 de Oct. de 2015
Editada: Guillaume el 20 de Oct. de 2015
Like every function in matlab sort works on a copy of the original data. It never modifies the input.
If you meant something else, you need to rephrase your comment. My answer will give you the least 8 numbers without modifying the input.
"i want the exact position numbers without sorting". Asking for the least (or most) n anything means that by definition you've assigned an ordering to the set. Finding the order of a set is called sorting. You can't do one without the other.
sorry, i didn't see properly. Its working for me. Thank u very much.

Iniciar sesión para comentar.

Más respuestas (1)

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by