Is there a function or method that rounds a value towards a entries of a vector?

1 visualización (últimos 30 días)
Say we have the value 5.5. If we do floor(5.5), we get 5. If we do ceil(5.5), we get 6. Is there a function or method that can be used to round 5.5 towards entries of a vector you provide? Take this example:
We have the vector 1.25:10.25 = 1.25, 2.25, 3.25, 4.25, 5.25, 6.25... we also have the value 5.5.
Is there a function that rounds 5.5 to a certain direction using the numbers in this vector?
"function"(5.5,1.25:10.25) = 5.25 And is there another function which can go the other direction? "otherfunction'(5.5, 1.25:10.25) = 6.25
Thanks for your help.

Respuesta aceptada

Evan
Evan el 15 de Jul. de 2013
v = 1.25:10.25;
x = 5.5;
v(find(x-v>0,1,'last')); %round to nearest lower
v(find(x-v<0,1,'first')); %round to nearest higher

Más respuestas (1)

Image Analyst
Image Analyst el 30 de Mayo de 2014

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by