# Remove corresponding values in two arrays

27 visualizzazioni (ultimi 30 giorni)
Ana Gabriela Guedes il 14 Apr 2021
Commentato: Adam Danz il 14 Apr 2021
Hi!
I have a vector with a lot of numbers, for example A = [9,1,5,2,3,2] and B = [12,23,41,4,10,6] (for example) and I want to remove all the values that are different from 1,2,5 or 9 and the correspondent elements in B. In this case I would want to remove the 3 in A and the 10 in B, ending uo with: new_A = [9,1,5,2,2] and new_B = [12,23,41,4,6]. For removing the values in A I'm doing the following but I dont know how to do remove the elenments in B.
(I want to apply this to vectors with hundreds of values so I cannot remove that separately)
A = [9,1,5,2,3,2] ;
B = [12,23,41,4,10,6];
x = [1,2,5,9,]; % values to keep in A
new_A = A(ismemmber(A,x));
How can I do this easily?
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Adam Danz il 14 Apr 2021

Accedi per commentare.

### Risposta accettata

David Hill il 14 Apr 2021
A = [9,1,5,2,3,2] ;
B = [12,23,41,4,10,6];
x = [1,2,5,9,]; % values to keep in A
new_A = A(ismember(A,x));
new_B = B(ismember(A,x));
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Ana Gabriela Guedes il 14 Apr 2021
thank you a lot!! It works just like that :)

Accedi per commentare.

### Più risposte (1)

Bob Thompson il 14 Apr 2021
Modificato: Bob Thompson il 14 Apr 2021
How can I do this easily?
Please feel free to elaborate what 'easily' means to you. Your logic for A seems pretty well set up to me. The only thing I can think of to make it more 'easy' would be how you're generating x, but I know nothing about that, so I can't help.
That said, getting B to be reduced is actually just as simple.
new_B = B(ismember(A,x));
The same logic for A can be applied to B because ismember(A,x) produces a logic array, rather than the specific values, and so you can similarly using it to indicate which elements of B you want.
This does require A and B to be the same size (I believe).
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Ana Gabriela Guedes il 14 Apr 2021
thank you a lot!! It works just like that :)

Accedi per commentare.

### Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by