Find the two nearest points from an array given a value

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?

 Accepted Answer

[~,k] = mink(abs(A-0.1400),2);

7 Comments

Nom
Nom on 8 Oct 2019
Edited: Nom on 8 Oct 2019
Thank you so much!
It worked perfectly, simple solution.
Would you be able to explain exactly how it worked?
Sure, first you subtract 0.14 from A, so now you are trying to find the values closest to zero. Then, just taking the minimum value would only serve to find the minimum value in A, since it would be most negative. But we want closest to zero, not smallest overall. So take the absolute value and then find the minimum value. mink finds the k minimum values.
thank you so much,
Also I know this goes outside of the scope of this question but,
let's say I have
A = [0.1035
0.1062
0.1094
0.1121
0.1264
0.1334
0.1338]
The value I want the two closest points to is x = 0.1165
In this case it will choose 0.1094 and 0.1121. Which are both correct.
Is there anyway to have it pick one value from the negative side of x and one in the positive side of x?
So the answer should be 0.1121 and 0.1264?
Of course this is possible, but I will leave it to you to figure out. You'll have to tweak the code I provided, by calling it twice slightly differently. Drop the abs and think about using max instead of min. Draw the problem on a number line to help visualize it.
Hmmmmm, I can defintely see what we are trying to do. Because we removed the abs function. There will be a point where there is a change in the signs. (e.g. A-0.1400 will keep giving us a negative answer until we cross the positive side of x which would be any number larger than 0.1400).
In that case, to find the closest negative value:
[~,kNegative] = mink(abs(A-0.1165),1);
closest postive value:
[~,kPositive] = maxk(A-0.1165,1);
But weirdly, still not getting the right value from kPositive
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).
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!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2017b

Asked:

Nom
on 8 Oct 2019

Commented:

Nom
on 8 Oct 2019

Community Treasure Hunt

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

Start Hunting!