I would like to compare/match elements of 2 vector data, the goal is to select the elements which element-wise from the 2 vectors are different not greater than 3. Then output are the 2 new vector with have the same length and its elements are arranged in order. The size of the 2 vectors (before processed) is arbitrary.
Ex. A = [11 38 49 84 96 117 157 176 200]
B = [10 28 37 48 84 157 175 200]
Compare by element which abs(A(i) - B(j)) <= 3 Then the output should be
A_new = [11 38 49 84 157 176 200]
B_new = [10 37 48 84 157 175 200]
Can anyone guide me how to write MATLAB code for this problem? I used 2 nested loop but got stuck with empty set fault.
Thank you.

 Réponse acceptée

Jos (10584)
Jos (10584) le 8 Avr 2014
A = [11 38 49 84 96 117 157 176 200]
B = [10 28 37 48 84 157 175 200]
D = abs(bsxfun(@minus, A(:), B(:).')) % find difference of all combinations of A and B
[iA,iB] = find(D <= 3) % which combos meet the criterion
A_new = A(iA)
B_new = B(iB)

1 commentaire

Bundit Buddhahai
Bundit Buddhahai le 9 Avr 2014
Hi Jos(10584), Many thanks for your advice. I got my output when using bsxfun. It's very useful function. Regards, Bundit

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 6 Avr 2014
Seems ambiguous to me. What would you do in this case:
Ex. A = [11 38 39 84 96 117 157 176 200]
B = [10 28 37 48 84 157 175 200]
If the 38 moves to position 2 to be associated with the 38, then what does 39 get associated with? Is it compared to the 37, or with the 48 because the values "slide over."
What about these arrays:
Ex. A = [1 1 1 1 1 1 1]
B = [2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3]

3 commentaires

Bundit Buddhahai
Bundit Buddhahai le 7 Avr 2014
Modifié(e) : Image Analyst le 7 Avr 2014
The original data of A and B are:
A = [11 38 49 84 96 117 157 176 200]
B = [10 28 37 48 84 157 175 200]
The different value of each element in the vector is always greater than the matching threshold value(in this case, matching threshold = 3). To compare and match data of the 2 vectors, the matched output of 2 vectors should be:
A_new = [11 38 49 84 157 176 200]
B_new = [10 37 48 84 157 175 200]
Each element with the same index of the 2 vectors has different value not greater than 3.
Image Analyst
Image Analyst le 7 Avr 2014
Where did 49 come from? That was NOT in my example. Anyway, why not just do a pair of nested for loops?
Bundit Buddhahai
Bundit Buddhahai le 8 Avr 2014
The third element of A is '49' not '39' (as shown in the first quote). Each adjacent element has different value more than 3.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by