필터 지우기
필터 지우기

Removing a specific number from vector

조회 수: 65 (최근 30일)
Ulrik Nash
Ulrik Nash 2011년 6월 17일
댓글: Walter Roberson 2022년 8월 8일
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.

답변 (4개)

Gurudatha Pai
Gurudatha Pai 2011년 6월 17일
A = 4;
Z = [1 9 2 3 4 5]
Z1 = Z(find(Z~=A))
that should work

Walter Roberson
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 ==

Tiasa Ghosh
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
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
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
Richard Barrett-Jolley 2018년 7월 13일
Yes, that would work, but I would recommend simply:
Z=Z(Z~=A);
  댓글 수: 4
huda nawaf
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
Walter Roberson
Walter Roberson 2022년 8월 8일
Z = Z(~ismember(Z, A));

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by