필터 지우기
필터 지우기

Align/intersect two vectors of different size and non-exact matches then trim the edges

조회 수: 7 (최근 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))

채택된 답변

Jan
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)
A = 1×5
21.0000 26.0000 30.8000 34.7000 39.9000
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)
newA = 1×3
21.0000 26.0000 30.8000
B = B(isnan(maskB))
B = 1×3
20.6000 25.0000 30.1000
  댓글 수: 1
AbioEngineer
AbioEngineer 2022년 6월 5일
Thank you! I have edited the question to include the criteria for choosing the elements in A and B. I basically want a "rough/fuzzy match" algorithm that finds the elements in one vector that are similar to another vector. matchpairs does this pretty well, but not for all scenarios

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by