필터 지우기
필터 지우기

Finding closest points to a given range in matrix

조회 수: 2 (최근 30일)
tethys
tethys 2011년 12월 13일
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.

채택된 답변

Sean de Wolski
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,:)
  댓글 수: 1
tethys
tethys 2011년 12월 13일
it is working well. Thank you for your suggestions and time!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Fangjun Jiang
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

Mads
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.

tethys
tethys 2011년 12월 13일
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
Sean de Wolski
Sean de Wolski 2011년 12월 13일
BSXFUN is in kaan's future!
tethys
tethys 2011년 12월 13일
Yes, definetely I am looking for closest to (0, 0.28) and its corresponding z. Sorry for the confusing

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by