Effacer les filtres
Effacer les filtres

ismember and if not working

15 vues (au cours des 30 derniers jours)
Khairul Nur
Khairul Nur le 22 Juil 2021
Commenté : Khairul Nur le 22 Juil 2021
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

Réponse acceptée

Simon Chan
Simon Chan le 22 Juil 2021
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 commentaire
Khairul Nur
Khairul Nur le 22 Juil 2021
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by