フィルターのクリア

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

1 回表示 (過去 30 日間)
Feng Cheng Chang
Feng Cheng Chang 2012 年 12 月 1 日
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.

採用された回答

Azzi Abdelmalek
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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 1 日
I suggest Walter's answer
Feng Cheng Chang
Feng Cheng Chang 2012 年 12 月 1 日
Thanks for your suggestion.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 12 月 1 日
r = setdiff(p, q);
  3 件のコメント
Walter Roberson
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.
Matt Fig
Matt Fig 2012 年 12 月 1 日
Not in 2011b....

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by