Align/intersect two vectors of different size and non-exact matches then trim the edges
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Jan
le 5 Juin 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)
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))
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!