How to eliminate the elements in an array from another array?

Hi I have two arrays:
a=[ 1 2 3 5 6 7 8 9 100];
b=[1 2 3];
I want to eliminate the elements in b from a and gives me:
c=[5 6 7 8 9 100]
How am I going to do this? Thanks in advance.

답변 (2개)

Guillaume
Guillaume 2014년 10월 4일
편집: Guillaume 2014년 10월 4일
Assuming there's no repeating elements in a:
c = setdiff(a, b); %will also remove duplicates in a
If you have repeating elements and want to keep the duplicates:
c = a(~ismember(a, b));

댓글 수: 1

I don't think the second one could keep repeating elements...

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

Zoltán Csáti
Zoltán Csáti 2014년 10월 4일
Simply,
c = a;
c(b) = [];

댓글 수: 1

Guillaume
Guillaume 2014년 10월 4일
편집: Guillaume 2014년 10월 4일
No! That is completely wrong and only works because elements [1 2 3] also happen to be at index [1 2 3] in a. Try it with b = [100]

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2014년 10월 4일

댓글:

JC
2019년 6월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by