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

28 visualizaciones (últimos 30 días)
Grace
Grace el 4 de Oct. de 2014
Comentada: JC el 2 de Jun. de 2019
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.

Respuestas (2)

Guillaume
Guillaume el 4 de Oct. de 2014
Editada: Guillaume el 4 de Oct. de 2014
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 el 4 de Oct. de 2014
Simply,
c = a;
c(b) = [];
  1 comentario
Guillaume
Guillaume el 4 de Oct. de 2014
Editada: Guillaume el 4 de Oct. de 2014
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]

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by