How do I get the closest values of an array stored for a particular input ?

1 次查看(过去 30 天)
Hello all,
I have an array A = [180 170 160 150 140 130] and b = 165... how do I determine the closest positions and values of b from A i.e. what function should I use to get the answer as 170 & 160.
Thank you for your help in advance.

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2016-8-17
编辑:Azzi Abdelmalek 2016-8-17
A = [180 170 160 150 140 130]
b=165
a1=min(A(A>b))
a2=max(A(A<b))

更多回答(1 个)

Thorsten
Thorsten 2016-8-17
[~, ind]= sort(abs(A-b));
A(ind(1:2))

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by