Output of a vector which excludes the entries of the second vector

2 vues (au cours des 30 derniers jours)
Hello,
Problem: Given two distinct integer vectors p and q. I'd like to get a new vector r, that is p excluding the entries of q.
For example,
p = [1 2 3 4 5 6 7 8], q = [2 5 7 9], then r = [1 3 4 6 8].
Hope to have a code in a singer line. Thanks.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 1 Déc 2012
Modifié(e) : Azzi Abdelmalek le 1 Déc 2012
p = [1 2 3 4 5 6 7 8],
q = [2 5 7 9],
out=p(~ismember(p,q))
  3 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 1 Déc 2012
I suggest Walter's answer
Feng Cheng Chang
Feng Cheng Chang le 1 Déc 2012
Thanks for your suggestion.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 1 Déc 2012
r = setdiff(p, q);
  3 commentaires
Walter Roberson
Walter Roberson le 1 Déc 2012
Note that the two answers are slightly different, having to do with the ordering of the outputs if the first vector is not sorted. setdiff() also has a 'stable' option to get the outputs in the original order.
Matt Fig
Matt Fig le 1 Déc 2012
Not in 2011b....

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing 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!

Translated by