method to check for similarity using if...else loop

I have got a cell arrays as like -
selected_item = 'item3';
total_items = {'item1', 'item2', 'item3', item4', 'item5', 'item6'}
items are char.
'item1' and 'item4' are similar - undergoes same operation.
'item2' , 'item3' , 'item6' are similar - undergoes same operation.
'item5' is unique - undergoes different operation.
Now I want to perform operation only on 'item3' since that come into selected from previous codes.
I was trying to write something like this -
if selected_item == 'item2' || 'item3' || 'item6' % below code for item2, item3 and item6
........
........
........
else if selected_item == 'item1' || 'item4' % below code for item1 and item4
........
........
........
else % for item5
.........
end
I know above code does not work, its for explanation how i would like to do. Also it might be possible to put similar items in single variable -
type1_item = {'item2', 'item3', 'item6'};
than we can directly look if whatever comes in selected_item is present in type1_item or not. Is there any other method to resolve these??

 Respuesta aceptada

Guillaume
Guillaume el 23 de Mzo. de 2018
Use ismember to test for set membership, not == or strcmp:
if ismember(selected_item, {'item2', 'item3, 'item6'})
%do something
If you use strcmp then you get a logical array, which you could reduce to a scalar value with any:
if any(strcmp(selected_item, {'item2', 'item3, 'item6'}))
%do something
ismember is more straightforward.

1 comentario

Ghenji
Ghenji el 23 de Mzo. de 2018
well i have tried both the methods and don't find much difference between them. There would be a difference off course else it would not exist. Anyways thanks to you guys again.

Iniciar sesión para comentar.

Más respuestas (1)

Birdman
Birdman el 23 de Mzo. de 2018
Editada: Birdman el 23 de Mzo. de 2018
The best workaround for this situation is to use strcmp:
if strcmp(selected_item,{'item2', 'item3', 'item6'})
%do sth
elseif strcmp(selected_item,{'item1', 'item4'})
%do sth
else
%do sth
end
Do not use == operator for string comparison.
[Edited]
if any(strcmp(selected_item,{'item2', 'item3', 'item6'}))
%do sth
elseif any(strcmp(selected_item,{'item1', 'item4'}))
%do sth
else
%do sth
end

4 comentarios

Guillaume
Guillaume el 23 de Mzo. de 2018
Editada: Guillaume el 23 de Mzo. de 2018
Do not use == operator for string comparison
Since the introduction of the string class in R2016b, you have to be careful about the term string. Comparison with the string class is actually done with ==. However, the OP does not have strings but char arrays. And indeed,
Do not use == operator for char arrays comparison.
edit: Oh, and in this instance strcmp is the wrong function, (it'll return an array of logical). ismember is the right function to use.
Maybe it can be improved by adding any as follows:
if any(strcmp...
Ghenji
Ghenji el 23 de Mzo. de 2018
Editada: Ghenji el 23 de Mzo. de 2018
would these work too?
if strcmp(selected_item, type1_item) % item2,3,6 are elements of same variable
Birdman
Birdman el 23 de Mzo. de 2018
Editada: Birdman el 23 de Mzo. de 2018
Yes but it is missing, you should add any since containing at least one variable is enough for you:
if any(strcmp(selected_item,{'item2', 'item3', 'item6'}))
%do sth
elseif any(strcmp(selected_item,{'item1', 'item4'}))
%do sth
else
%do sth
end

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 23 de Mzo. de 2018

Comentada:

el 23 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by