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 日

1 投票

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 件のコメント

JC
JC 2019 年 6 月 2 日
I don't think the second one could keep repeating elements...

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

Zoltán Csáti
Zoltán Csáti 2014 年 10 月 4 日

0 投票

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 ExchangeMatrix 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