Finding closest points to a given range in matrix

2 views (last 30 days)
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

Sean de Wolski
Sean de Wolski on 13 Dec 2011
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,:)

More Answers (3)

Fangjun Jiang
Fangjun Jiang on 13 Dec 2011
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

Mads
Mads on 13 Dec 2011
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.

tethys
tethys on 13 Dec 2011
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
tethys
tethys on 13 Dec 2011
Yes, definetely I am looking for closest to (0, 0.28) and its corresponding z. Sorry for the confusing

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!