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

1 view (last 30 days)
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.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 1 Dec 2012
Edited: Azzi Abdelmalek on 1 Dec 2012
p = [1 2 3 4 5 6 7 8],
q = [2 5 7 9],
out=p(~ismember(p,q))

More Answers (1)

Walter Roberson
Walter Roberson on 1 Dec 2012
r = setdiff(p, q);
  3 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!