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

24 次查看(过去 30 天)
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));

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]

请先登录,再进行评论。

类别

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