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?

 採用された回答

Birdman
Birdman 2018 年 1 月 5 日
編集済み: Birdman 2018 年 1 月 5 日

23 投票

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 件のコメント

Jens Keijser
Jens Keijser 2018 年 1 月 5 日
Thank you!
Jan
Jan 2018 年 1 月 5 日
@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.
Jan
Jan 2018 年 1 月 8 日
@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.
Rob
Rob 2020 年 4 月 9 日
@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.
Ganesh Kini
Ganesh Kini 2020 年 4 月 16 日
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
Walter Roberson
Walter Roberson 2020 年 4 月 16 日
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);
Ganesh Kini
Ganesh Kini 2020 年 4 月 17 日
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?
Walter Roberson
Walter Roberson 2020 年 4 月 18 日
peiod_arr(idx)
Ganesh Kini
Ganesh Kini 2020 年 4 月 18 日
編集済み: Ganesh Kini 2020 年 4 月 18 日
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
Walter Roberson
Walter Roberson 2020 年 4 月 18 日
please post your current code. The code that I guessed that you had worked properly for me.
Ganesh Kini
Ganesh Kini 2020 年 4 月 28 日
編集済み: Ganesh Kini 2020 年 4 月 28 日
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
Walter Roberson
Walter Roberson 2020 年 4 月 28 日
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) ?
Jon Martínez Rico
Jon Martínez Rico 2021 年 5 月 27 日
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!
Jan
Jan 2021 年 5 月 27 日
@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)
Jon Martínez Rico
Jon Martínez Rico 2021 年 5 月 27 日
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 2023 年 8 月 29 日
編集済み: Umesh Gautam 2023 年 8 月 29 日
Thanks @Birdman, code is working great...even one can find the array of values.
Andres Felipe
Andres Felipe 2023 年 12 月 1 日
Thankss.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 1 月 5 日

3 投票

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
Atique Barudgar
Atique Barudgar 2019 年 11 月 8 日

0 投票

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 件のコメント

Nicolas Hofer
Nicolas Hofer 2021 年 10 月 8 日
idx stand for index. search for indexing in matlab for further explanation

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by