What is an efficient way to find the smallest distance from a number lets say 2.5
in an array A in MATLAB?
The problem I have is that while I use the function min
min(abs(A - 2.5))
I get an answer eg 0.0053 I do not know what is the index of the number that gives the smallest difference after the subtraction.
I tried to add again the number so I get 2.5053 and when I try to do
find(2.5053)
I get:
Empty matrix: 1-by-0
Just use the second output of min
:
[value, index] = min(abs(A - 2.5));
Adding the number and then using find
has several problems:
abs
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.