Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

If I have a vector {1,2,3}, I want to write an If statement, where if one of the positions is "1", it will perform a certain command.

test = {1, 2, 3}
# == 1 & /@ test 
If[# == 1 & /@ test == True, Print["Yes"], Print["no"]]

I get this as the output:

If[{True, False, False} == True, Print["Yes"], Print["no"]]

What am I doing wrong? I am new to Mathematica.

share|improve this question

marked as duplicate by Kuba, Yves Klett, m_goldberg, RunnyKine, Sjoerd C. de Vries Apr 29 at 21:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Use MemberQ to test if an element is in the list. –  Szabolcs Apr 29 at 18:31
1  
You should study the answers to this question Map a function across a list conditionally. In fact this is a duplicate. –  Artes Apr 29 at 18:34

1 Answer 1

There are a number of different solutions. Here are a few:

test = {1, 2, 3}
# == 1 & /@ test

If[Or @@ Thread[# == 1 & /@ test == True], Print["Yes"], Print["no"]]
If[Or @@ (# == 1 & /@ test), Print["Yes"], Print["no"]]
If[MemberQ[test, 1], Print["Yes"], Print["No"]]

In my opinion these are in increasing order of clarity and good practice.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.