0

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.

CaroKuz
  • 1
  • 1

1 Answers1

0

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.

evanb
  • 6,026
  • 18
  • 30