Finding closest points to a given range in matrix

Hi,
I have a 3 column matrix, x,y,and z respectively. I want to obtain the value of z corresponds to (closest point to 0.00 in x), and closest point to 0.29 in y) (both conditions must be satisfied in the same row).
To sum , for example, x range is between 0.00 and 0.02, and that of y is between 0.28 and 0.29). At the end, I may obtain the values as x=0.0005, its corresponding y=0.28 and (z=-0.3355)
I investigated in the forum but could not find a solution. Could you kindly give me some advice for this?
Thanks in advance.

 Accepted Answer

A =[ 0.375 0.279 0.366
0.004 0.256 0.321
0.004 0.266 0.322
0.004 0.276 0.333
0.004 0.286 0.338
0.004 -0.687 0.211
0.004 -0.677 0.216
0.486 -0.687 0.201
0.787 -0.697 0.146
1.168 -1.229 0.050
-0.588 -0.587 0.080
-0.678 -0.988 0.036
-0.839 0.065 0.062];
To find the minimum sum of absolute differences.
goal = [0 0.28];
[~, idx] = min(sum(abs(bsxfun(@(minus,A(:,1:2),goal)),2));
A(idx,:)
To find the minimum vector magnitude in 2d space:
goal = [0 0.28];
r = bsxfun(@minus,A(:,1:2),goal);
[~, idx] = min(hypot(r(:,1),r(:,2)));
A(idx,:)

1 Comment

it is working well. Thank you for your suggestions and time!

Sign in to comment.

More Answers (3)

The closet point to 0 in x might have one point. The closet point to 0.29 might also have one point. What if they are not in the same row? Does't it mean you don't have a solution.
xyz=rand(20,3);
[MinX,Indx]=min(abs(xyz(:,1)))
[MinY,IndY]=min(abs(xyz(:,2)-0.29));
if MinX~=MinY
disp('no solutin');
else
FoundZ=xyz(IndX,3);
end
If A is your matrix you could use:
[~,b] = min(abs(A(:,1))+abs(A(:,2)-0.29))
where b is the rows position which satisfies being the minimum absolute distance to the point.
First of all, thank you for your time.
I tried both of them , but "there is no solution". Yet, for instance when I evaluate my data I have [0,0040 0,2759 -0,3329], I want to obtain data like this x is closest to zero, y is closest to 0.29 (intersection)which give me z. Regarding values they do not need to be same.
I am still trying to develop your codes.

6 Comments

Do you understand my point in my answer? Or maybe you didn't describe your question correctly. Could you provide a set of example data (the nx3 matrix) and then explain the expected output?
Maybe I think I could not describe my question properly,Here is the set of data:
0,375 0,279 0,366
0,004 0,256 0,321
0,004 0,266 0,322
0,004 0,276 0,333
0,004 0,286 0,338
0,004 -0,687 0,211
0,004 -0,677 0,216
0,486 -0,687 0,201
0,787 -0,697 0,146
1,168 -1,229 0,050
-0,588 -0,587 0,080
-0,678 -0,988 0,036
-0,839 0,065 0,062
For me, I want to find "0,004 0,276 0,333", because x is closest to zero, and y is closest to 0.28.
For "0,375 0,279 0,366", y is closest to 0.28 but x is not closest to zero. It does not make sense to me.
I hope I can explain better now. Thank you again for your valuable time.
The code you suggested me can find the closest for x and y for the range I aspire; x has to be closest one to zero, than y has to be closest to 0.28 but within minimum x vector.
So are you looking for a point of (x,y) which is the closet to (0,0.28), which means sqrt(x^2+(y-0.28)^2)? That is different than "x close to 0" and "y close to 0.28".
BSXFUN is in kaan's future!
Yes, definetely I am looking for closest to (0, 0.28) and its corresponding z. Sorry for the confusing

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!