Align/intersect two vectors of different size and non-exact matches then trim the edges
조회 수: 6 (최근 30일)
이전 댓글 표시
I have two vectors, a and b, that contain various numbers of elements. Each vector has values that are roughly, but not exactly, 5 apart.
A=[21 26 30.8 34.7 39.9]
B=[11 15.4 20.6 25 30.1 ]
EDIT: I want to keep the elements in A and B that "overlap", but the issue is they don't have exact matches so I can't use ismember, interesect, or find. That is, I can visually tell that A(1:3) corresponds with B(3:5).
The result should be:
newA=[21, 26, 30.8]
newB=[20.6, 25, 30.1]
There may also be a case where there is an extra value in one of the vectors that is smaller than 5 apart. e.g. if A was instead A=[21 26 30.8 34.7 36 39.9] with "36" in the second to last position, which I want to ignore.
I have already tried
C=abs(A(:)-B(:)');
result=sortrows( matchpairs(C,max(C(:))) ,1)
newA=A(result(:,1))
newB=B(result(:,2))
댓글 수: 0
채택된 답변
Jan
2022년 6월 5일
Problem 2:
A = [21 26 30.8 34.7 36 39.9];
limit = 3.9;
match = true(size(A));
a = A(1);
for k = 2:numel(A)
if A(k) - a > limit
a = A(k);
else
match(k) = false;
end
end
A = A(match)
Problem 1 requires a lot of guessing, because you did not mention, how the elements of A and B are selected. Remember that there is an infinite number of methods to create newA, newB based an A and B.
A = [21 26 30.8 34.7 39.9];
B = [11 15.4 20.6 25 30.1];
limit = 2; % ??!?
keepA = false(size(A));
keepB = false(size(B));
maskB = B;
for iA = 1:numel(A)
[d, iB] = min(abs(A(iA) - maskB));
if d < limit
keepA(iA) = true;
maskB(iB) = NaN;
end
end
newA = A(keepA)
B = B(isnan(maskB))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!