how to get the min in an array and return its position?

13 visualizaciones (últimos 30 días)
JacobM
JacobM el 23 de Sept. de 2016
Editada: Stephen23 el 23 de Sept. de 2016
I want to check the min in a matrix and return its position, Ex. x=[1 3 1 1], the code will check the min which is '1' and return its position (number of columns), Y=[1 3 4]
Any hint?

Respuestas (2)

Stephen23
Stephen23 el 23 de Sept. de 2016
Editada: Stephen23 el 23 de Sept. de 2016
>> x = [1,3,1,1];
>> minval = min(x)
minval = 1
>> idx = find(x==minval)
idx =
1 3 4
This kind of basic MATLAB usage is covered very well in the introductory tutorials:
  2 comentarios
JacobM
JacobM el 23 de Sept. de 2016
Great. what about if I just to return one minimum?
So in my example, I want the result to be Y=[1] which is the column position of first minimum,
Stephen23
Stephen23 el 23 de Sept. de 2016
Editada: Stephen23 el 23 de Sept. de 2016
[minval,idx] = min(x)

Iniciar sesión para comentar.


Star Strider
Star Strider el 23 de Sept. de 2016
For one (or the first) minimum, use the min function with two outputs:
x=[1 3 1 1];
[xmin,idx] = min(x)
xmin =
1
idx =
1
The first output is the value of the minimum, the second is the first occurrence of the minimum in the vector.

Categorías

Más información sobre Elementary Math 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