How to get 2nd minimun value from matrix?

2 views (last 30 days)
Zahid Iqbal Rana
Zahid Iqbal Rana on 29 Jan 2015
Commented: Guillaume on 29 Jan 2015
I want to get the 2nd minimum number from a matrix. Elements in matrix are random and i don't want to sort them. for example
A=[2 6 83 14 29;17 42 31 90 23;17 52 43 81 29;48 11 90 21 58];

Answers (2)

Jeremy
Jeremy on 29 Jan 2015
It is a little cumbersome but for any array x, this should work:
min(x(x~=min(x(:))))

Guillaume
Guillaume on 29 Jan 2015
I don't want to sort them.
Asking for the nth element is by definition sorting them.
One possible way (which obviously does a sort):
A = [2 6 83 14 29;17 42 31 90 23;17 52 43 81 29;48 11 90 21 58];
u = unique(A);
a2 = u(2) %achtung baby!
  2 Comments
Guillaume
Guillaume on 29 Jan 2015
Huh! And what is the 'correct result' you expect?

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!