フィルターのクリア

Find the two nearest points from an array given a value

37 ビュー (過去 30 日間)
Nom
Nom 2019 年 10 月 8 日
コメント済み: Nom 2019 年 10 月 8 日
Hello,
Currently I have an 87x1 array called A.
A = 0.1334
0.1338
0.1348
0.1386
0.1444
0.1448
0.1452
0.1459
0.1469
0.1478
0.1488
I now have a value of 0.1400
What I want from A is the two nearest values to this number. In this case, it should be 0.1386 and 0.1444
I have the following code below which I have been trying to get to work:
k = dsearchn(A,0.1400)
This gives me 4 as the output which makes sense as the 4th row in array A has 0.1386 which is one of the closest.
However, how am I able to get the second closest of 0.1444?

採用された回答

Daniel M
Daniel M 2019 年 10 月 8 日
編集済み: Daniel M 2019 年 10 月 8 日
[~,k] = mink(abs(A-0.1400),2);
  7 件のコメント
Daniel M
Daniel M 2019 年 10 月 8 日
Sorry, my hints were poor.
A = [0.1035
0.1062
0.1094
0.1121
0.1264
0.1334
0.1338
0.1165];
res = A-0.1165;
res2 = res;
% find closest below
res(res>=0) = nan;
[~,kNeg] = max(res);
% find closest above
res2(res2<0) = nan;
[~,kPos] = min(res2);
Note how I handled it for when one value is A equals x (you can choose differently if you wish).
Nom
Nom 2019 年 10 月 8 日
Not at all, thank you for helping me out in this matter.
I apologize that I didn't catch on to the code, I really appreaciate this!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by