How can I extract coordinates (x,y,z) from a matrix that match coordinates in another matrix

Hi
I have a matrix (A) containing columns for coordinates (x,y,z) and a columns with simulated values (v) for each point. I also have a matrix (B) containing coordinates (x,y,z)for a reference data set which is much smaller.
I would like to remove all rows (points) in matrix A which don't have the same coordinates as in matrix B so that i have a matrix containing x,y,z,v for each point that is included in matrix B.
I also like to state that i have tried to use ismember and intersect for this but it does not give me the same amount of points that is given in matrix B.
Any easy way to solve this?

 채택된 답변

I think that the problem might be that your number are doubles. I suggest an approach based on distance.
You could first use the function pdist2(A,B) which will compute the distance between every point of A and B in a matrix. Then filter the results.
A = [1, 1 , 1;
2, 2 , 2;
3, 3 , 3;
4, 4, 4;
5, 5, 5];
B = [3 , 3, 3;
5, 5, 5];
tol = eps;
dist = pdist2(A,B);
samePoints = dist < eps;
rowIndexSamePoint = find(logical(sum(samePoints,2)) > 0);
AFilt = A(rowIndexSamePoint,:); % A and B are the same matrix

댓글 수: 4

Hi, Thank you for a quick answer.
Although my problem still remains. My matrix A look like your exempel but also have a fourth column with the values that i want to extract. So it looks somehting like this:
A = [1,1,1,7;2,2,2,4;3,3,3,9;4,4,4,2;5,5,5,6]
and B = [3,3,3;5,5,5]
What i want is to extract value 9 and 6 from the fourth column in A.
Any idea?
Thanks
With the matrices in your case:
tol = eps;
dist = pdist2(A(:,1:3),B);
samePoints = dist < eps;
rowIndexSamePoint = find(logical(sum(samePoints,2)) > 0);
AVals = A(rowIndexSamePoint,4);
Yes, It works well!
Thank you for all your help, really appreciate it.
I am glad to hear that. Happy coding!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2018년 10월 12일

댓글:

2018년 10월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by