필터 지우기
필터 지우기

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))
  댓글 수: 2
Raahul Palanivel Uma
Raahul Palanivel Uma 2016년 8월 17일
thank you...the function works perfectly :)
Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 17일
It doesn't work for this example
A = [180 170 162 161 150 140 130]
b=165
[~, ind]= sort(abs(A-b));
A(ind(1:2))
The result is
162 161

댓글을 달려면 로그인하십시오.

카테고리

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