필터 지우기
필터 지우기

ismember and if not working

조회 수: 15 (최근 30일)
Khairul Nur
Khairul Nur 2021년 7월 22일
댓글: Khairul Nur 2021년 7월 22일
hi, basically i want to check whether the array "A" contains specific value or not (0,2 and 3), if the array lack of specific value, i would like to add into the array and do some counting. Here is my code. Not error but i didnt into the elseif condition.
The specific value are stored in array_include_exclude array.
Hope my explainantion is clear. Please help to solve this. TQIA
array_include_exclude=[0 2 3]'
array_include_only=[0 2]'
array_exclude_only=[0 3]'
A=[0 2]'
count1=0
count2=0
count3=0
if A ismember(array_include_exclude)
count1=count1+1
elseif A ismember(array_include_only)
A=[A 3]
count2=count2+1
elseif A ismember(array_exclude_only)
A=[A 2]
count3=count3+1
end

채택된 답변

Simon Chan
Simon Chan 2021년 7월 22일
Please refer to the usage of ismember below, so location of variable A is not correct,
On the other hand, rearrange the order otherwise the first condition always satisfy and exit the loop.
Syntax
if ismember(A,array_include_only)
A=[A; 3] % Modify to become a column matrix
count2=count2+1
elseif ismember(A,array_exclude_only)
A=[A; 2]
count3=count3+1
elseif ismember(A,array_include_exclude)
count1=count1+1
end
  댓글 수: 1
Khairul Nur
Khairul Nur 2021년 7월 22일
the code has some error on the array dimension and i edit it and working. Thanks Simon!
array_include_exclude=[0 2 3]
array_include_only=[0 2]
array_exclude_only=[0 3]
A=[0 2]
count1=0
count2=0
count3=0
if ismember(A,array_include_only)
A=[A 3] % Modify to become a column matrix
count2=count2+1
elseif ismember(A,array_exclude_only)
A=[A 2]
count3=count3+1
elseif ismember(A,array_include_exclude)
count1=count1+1
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by