Output of a vector which excludes the entries of the second vector
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Azzi Abdelmalek
2012-12-1
编辑:Azzi Abdelmalek
2012-12-1
p = [1 2 3 4 5 6 7 8],
q = [2 5 7 9],
out=p(~ismember(p,q))
3 个评论
更多回答(1 个)
Walter Roberson
2012-12-1
r = setdiff(p, q);
3 个评论
Walter Roberson
2012-12-1
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!