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?

 Respuesta aceptada

Birdman
Birdman el 5 de En. de 2018
Editada: Birdman el 5 de En. de 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 comentarios

Jens Keijser
Jens Keijser el 5 de En. de 2018
Thank you!
Jan
Jan el 5 de En. de 2018
@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.
Rob
Rob el 9 de Abr. de 2020
@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 el 16 de Abr. de 2020
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);
Ganesh Kini
Ganesh Kini el 17 de Abr. de 2020
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 el 18 de Abr. de 2020
peiod_arr(idx)
Ganesh Kini
Ganesh Kini el 18 de Abr. de 2020
Editada: Ganesh Kini el 18 de Abr. de 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
Walter Roberson
Walter Roberson el 18 de Abr. de 2020
please post your current code. The code that I guessed that you had worked properly for me.
Ganesh Kini
Ganesh Kini el 28 de Abr. de 2020
Editada: Ganesh Kini el 28 de Abr. de 2020
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 el 28 de Abr. de 2020
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!
Jan
Jan el 27 de Mayo de 2021
@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 el 27 de Mayo de 2021
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 el 29 de Ag. de 2023
Editada: Umesh Gautam el 29 de Ag. de 2023
Thanks @Birdman, code is working great...even one can find the array of values.
Andres Felipe
Andres Felipe el 1 de Dic. de 2023
Thankss.

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 5 de En. de 2018

3 votos

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 el 8 de Nov. de 2019

0 votos

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 comentario

Nicolas Hofer
Nicolas Hofer el 8 de Oct. de 2021
idx stand for index. search for indexing in matlab for further explanation

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Preguntada:

el 5 de En. de 2018

Comentada:

el 1 de Dic. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by