Removing a specific number from vector
159 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposte (4)
Walter Roberson
il 17 Giu 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 ==
0 Commenti
Tiasa Ghosh
il 25 Lug 2018
You can also use setdiff function.
a=4;
z=[1 9 2 3 4 5];
setdiff(z,a)
2 Commenti
Gospel Oh
il 15 Nov 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
il 26 Apr 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')
Richard Barrett-Jolley
il 13 Lug 2018
Yes, that would work, but I would recommend simply:
Z=Z(Z~=A);
4 Commenti
huda nawaf
il 7 Ago 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
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!