Find nearest value to specific number

Hello, I have an array with 20 values of steps per minute. I already know that the perfect outcome of one of these values is 33spm. But unfortunately 33spm is not in the array 34.8 is which is the closest to 33. What is the code to find the value closest to 33?
The ideal answer would be:
ClosestValue = 34.8
Could someone help me please?

 Accepted Answer

Birdman
Birdman on 5 Jan 2018
Edited: Birdman on 5 Jan 2018
Try the following approach:
a=[34.8 31 29 26.7 39.5];%dummy data
n=33;
[val,idx]=min(abs(a-n));
minVal=a(idx)
Edit after Jan's warning(multiple values)
a=[34.8 31.2 29 26.7 39.5];%dummy data
n=33;
[~,~,idx]=unique(round(abs(a-n)),'stable');
minVal=a(idx==1)

17 Comments

@Jens: What should happen, if multiple values have the same distance from the searched number? Birdman's code replies the first occurrence, which might be sufficient.
@Birdman: Alternatively without rounding:
dist = abs(a - n);
minDist = min(dist);
idx = find(dist == minDist);
Now minDist is a scalar, while idx contains all indices belonging to this value.
@Jan: Your code without rounding has the problem of hidden Round-off Errors. Thus, the result for this example is only the index 1 and not 1 and 2.
Hi,
I have a similar scenario
Does this work if i have an array whose matrix of 7 dimension ?
For example - peiod_arr(2,1,10,10,15,11,8)
Please let me know
Yes, no matter how many dimensions the array has, you can use the strategy
[min_dift, idx] = min(abs(TheArray(:) - TargetValue));
closest_value = TheArray(idx);
You might also want to get the indices:
[indices{1:ndims(TheArray)}] = ind2sub(size(TheArray), idx);
With an array that large, the possibility tends to grow that you might have multiple locations that are all the same distance. You should then consider:
dist = abs(TheArray - TargetValue);
min_dist = min(dist(:));
idx = find(dist == min_dist);
[indices{1:ndims(TheArray)}] = ind2sub(size(TheArray), idx);
Hi Walter,
I tried the code, and its not working as expected
I am not getting the value present in the array, I am getting the target value itself as the output.
COuld you please suggest some alternate solution?
Ganesh Kini
Ganesh Kini on 18 Apr 2020
Edited: Ganesh Kini on 18 Apr 2020
Hi Walter,
The thing is that I am actually dealing with decimal numbers and i want it to work for 0.001 precision.
For example if i have a target value of 26.145 and my period_arr has a value of 26.147 it should able to retrieve the value.
But it is not working as expected .
Please suggest
please post your current code. The code that I guessed that you had worked properly for me.
abc = period_fun(2,1,2,5,5,13,8);
%finding the nearest possible value
dist = abs(period_fun - abc);
[min_dist, idx] = min(dist(:));
nearestvalue = period_fun(idx);
actualidx= find(period_fun ==nearestvalue,1);
[p1,p2,p3,p4,p5,p6,p7] = ind2sub(size(period_fun), actualidx);
v1 = nw_vec(p5);
First Case
v1 is displaying as follows
0.80000 0.65000 0.75000
all these are in the nw_vec array but 0.8 is expected answer. I should get only 0.8 as the answer
Second case
v1 is displaying as follows
0.40000 0.55000
0.2 is expected but its not giving it as output. I should get only 0.2 as the answer
nw_vec is .vec file
the file is as follows
0.2, 0.36, 0.38, 0.40, 0.47, 0.5, 0.55, 0.65, 0.75, 0.8
I want only the p5 value for v1 = nw_vec(p5)
kindly help me
For first case, where you are getting three values, then what is size(actualidx) ? With the code you used, with find() with 1 as the second input, you should only get back one output, so ind2sub() should only be returning scalars. What is size(p5) ? What is class(nw_vec) ?
Hi! I am checking @Birdman's second answer, and in case I introduce in the series the value I am looking for explicitly, something does not work well.
a=[34.8 31.2 33 29 26.7 39.5];%dummy data
n=33;
[~,~,idx]=unique(round(abs(a-n)),'stable');
minVal=a(idx==1)
minVal =
34.8000 31.2000
Someone knows what happens?
Regards!
@Jon Martínez Rico: The 2nd code in this answer does not work. Use the simpler version:
dist = abs(a - n);
minDist = min(dist);
minIdx = (dist == minDist);
minVal = a(minIdx)
Yeah, I did it @Jan. It was just to check if I was missing something.
Thank you for your quick answer :)
Umesh Gautam
Umesh Gautam on 29 Aug 2023
Edited: Umesh Gautam on 29 Aug 2023
Thanks @Birdman, code is working great...even one can find the array of values.

Sign in to comment.

More Answers (2)

Use interp1 with 'nearest'
Or since the vector is small abs() the difference between the probe and the fixed values and min() that and take the second output of min() and use that to index the fixed values. This is not as convenient as interp1 but should be faster
I am not clear how Birdman SIr's answer came
when idx =1
then how a(idx)=34.8
I didnt got how and what is idx

1 Comment

idx stand for index. search for indexing in matlab for further explanation

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!