From a given number how to find the value closest to zero in a vector.

38 次查看(过去 30 天)
If a have a vector [93.60 119.15 136.19 191.55 238.39 268.20 302.27 340.60 383.18 425.77] and a number that every time is different.For example numb=410 i want to find the number 383.18 from the vector.

采纳的回答

Star Strider
Star Strider 2016-9-27
Use the find function:
vector = [93.60 119.15 136.19 191.55 238.39 268.20 302.27 340.60 383.18 425.77];
numb=410;
idx = find(vector <= numb, 1, 'last');
Result = vector(idx)
Result =
383.18

更多回答(1 个)

Kelly Kearney
Kelly Kearney 2016-9-27
Star's answer works in the values in the vector are sorted. If not, min might be a better choice:
vector = [93.60 119.15 136.19 191.55 238.39 268.20 302.27 340.60 383.18 425.77];
numb=410;
[~, imin] = min(abs(vector - num));
vector(imin)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by