ismember and if not working

15 次查看(过去 30 天)
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