필터 지우기
필터 지우기

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

조회 수: 1 (최근 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.

채택된 답변

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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by