필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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
Will Nitsch
Will Nitsch 2017년 5월 1일
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

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by