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

9 views (last 30 days)
AbioEngineer on 5 Jun 2022
Commented: AbioEngineer on 5 Jun 2022
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.
C=abs(A(:)-B(:)');
result=sortrows( matchpairs(C,max(C(:))) ,1)
newA=A(result(:,1))
newB=B(result(:,2))

Jan on 5 Jun 2022
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));
for iA = 1:numel(A)
[d, iB] = min(abs(A(iA) - maskB));
if d < limit
keepA(iA) = true;
end
end
newA = A(keepA)
newA = 1×3
21.0000 26.0000 30.8000
B = 1×3
20.6000 25.0000 30.1000
AbioEngineer on 5 Jun 2022
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

R2021b

### Community Treasure Hunt

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

Start Hunting!