Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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 Kommentar
Will Nitsch
Will Nitsch am 1 Mai 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

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by