Find nearest value to specific number
Mostrar comentarios más antiguos
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
Más respuestas (2)
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
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
el 8 de Oct. de 2021
idx stand for index. search for indexing in matlab for further explanation
Categorías
Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!