Removing a specific number from vector
254 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (4 件)
Walter Roberson
2011 年 6 月 17 日
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 件のコメント
Tiasa Ghosh
2018 年 7 月 25 日
You can also use setdiff function.
a=4;
z=[1 9 2 3 4 5];
setdiff(z,a)
2 件のコメント
Gospel Oh
2018 年 11 月 15 日
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
2019 年 4 月 26 日
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
2018 年 7 月 13 日
Yes, that would work, but I would recommend simply:
Z=Z(Z~=A);
4 件のコメント
huda nawaf
2022 年 8 月 7 日
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
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!