How can i find the position of nearest number "x" in a matrix "a"
5 views (last 30 days)
Show older comments
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
1 Comment
Answers (1)
Adam
on 11 Dec 2017
Edited: Adam
on 11 Dec 2017
[~,pos] = min( abs( a - x ) );
nearestNum = a( pos );
4 Comments
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!
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!