Borrar filtros
Borrar filtros

Removing a specific number from vector

159 visualizaciones (últimos 30 días)
Ulrik Nash
Ulrik Nash el 17 de Jun. de 2011
Comentada: Walter Roberson el 8 de Ag. de 2022
Hi Everyone,
Suppose I have the following
A = 4
and
Z = [1 9 2 3 4 5]
how do I remove the value A from Z, to get
Z = [1 9 2 3 5]
Regards,
Ulrik.

Respuestas (4)

Gurudatha Pai
Gurudatha Pai el 17 de Jun. de 2011
A = 4;
Z = [1 9 2 3 4 5]
Z1 = Z(find(Z~=A))
that should work

Walter Roberson
Walter Roberson el 17 de Jun. de 2011
Z(Z==A) = [];
Warning: if you have floating point numbers instead of integers, you will usually need to compare with a tolerance instead of ==

Tiasa Ghosh
Tiasa Ghosh el 25 de Jul. de 2018
You can also use setdiff function.
a=4;
z=[1 9 2 3 4 5];
setdiff(z,a)
  2 comentarios
Gospel Oh
Gospel Oh el 15 de Nov. de 2018
If the function were to be put into MatLab, the answer actually comes out to be an ordered version, missig the a:
ans = 1 2 3 5 9
close, but not quite
Paul Smits
Paul Smits el 26 de Abr. de 2019
You may pass 'stable' as additional input to setdiff, to prevent sorting:
a=4;
z=[1 9 2 3 4 5];
setdiff(z,a,'stable')

Iniciar sesión para comentar.


Richard Barrett-Jolley
Richard Barrett-Jolley el 13 de Jul. de 2018
Yes, that would work, but I would recommend simply:
Z=Z(Z~=A);
  4 comentarios
huda nawaf
huda nawaf el 7 de Ag. de 2022
HI
Im facing the same problem, but the size of A is more than one.
so Z=Z(Z~=A); does not work. Is there an alternative?
Thanks
Walter Roberson
Walter Roberson el 8 de Ag. de 2022
Z = Z(~ismember(Z, A));

Iniciar sesión para comentar.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by