Finding closest points to a given range in matrix
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Sean de Wolski
2011-12-13
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,:)
更多回答(3 个)
Fangjun Jiang
2011-12-13
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
0 个评论
Mads
2011-12-13
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!