How can i find the position of nearest number "x" in a matrix "a"

5 views (last 30 days)
for example : a = [4 8 3; 22 12 45; 88 74 65]; x = 66; the nearest number is obviously 65 how can i find it's position? i need a list of commands that gives me the nearest number to x and it's position

Answers (1)

Adam
Adam on 11 Dec 2017
Edited: Adam on 11 Dec 2017
[~,pos] = min( abs( a - x ) );
nearestNum = a( pos );
  4 Comments
mahmoud fekih
mahmoud fekih on 11 Dec 2017
when i mean position i mean i ant it to return the column and row numbers of the minimal value of the matrix and for my example the matrix is named d1 with 35 rows and 6 columns the minimal value is "0" i need it to return to me the column and row numbers of "0" (CHEK TH PICTURE)
Adam
Adam on 12 Dec 2017
You can achieve this easily from combining the above, though I don't know why you complicated your question by asking about nearest value if all you really want is the minimum of your matrix!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!