Borrar filtros
Borrar filtros

ismember and if not working

15 visualizaciones (últimos 30 días)
Khairul Nur
Khairul Nur el 22 de Jul. de 2021
Comentada: Khairul Nur el 22 de Jul. de 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

Respuesta aceptada

Simon Chan
Simon Chan el 22 de Jul. de 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 comentario
Khairul Nur
Khairul Nur el 22 de Jul. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by