Comparing matrices of different size in matlab and storing values that are close

1 view (last 30 days)
I have two matrices A and B. A(:,1) corresponds to an x-coordinate, A(:,2) corresponds to a y-coordinate, and A(:,3) corresponds to a certain radius. All three values in a row describe the same circle. Now let's say...
A =
[1,4,3]
[8,8,7]
[3,6,3]
B =
[1,3,3]
[1, 92,3]
[4,57,8]
[5,62,1]
[3,4,6]
[9,8,7]
What I need is to be able to loop through matrix A and determine if there are any rows in matrix B that are "similar" as in the x value is within a range (-2,2) of the x value of A (Likewise with the y-coordinate and radius).If it satisfies all three of these conditions, it will be added to a new matrix with the values that were in A. So for example I would need the above data to return...
ans =
[1,4,3]
[8,8,7]
Please help and thank you in advance to anyone willing to take the time!
  1 Comment
Will Nitsch
Will Nitsch on 1 May 2017
The following will scan through B, comparing each portion of A and B. If the criteria is met, then it will store the indices of the matching (within the range +/-2) values of A and B.
A = [[1,4,3];[8,8,7];[3,6,3]];
B = [[1,3,3];[1, 92,3];[4,57,8];[5,62,1];[3,4,6];[9,8,7]];
idx = [];
for i = 1:length(A)
for j = 1:1:length(B)
if(find(B(abs(B(j,1)-A(i,1))<=2 & abs(B(j,2)-A(i,2))<=2 & abs(B(j,3)-A(i,3))<=2)==1))
idx = [idx,[i;j]];
end
end
end
output:
idx =
1 2 % these are the indicies of A
1 6 % these are the corresponding indices of B

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements 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!