I have a mathematica list in which I want to replace all 2s with 1 and everything else to 0.
For example.
{0,1,2,3,2,3,4,5,2,2,6}
->
{0,0,1,0,1,0,0,0,1,1,0}
I assume it’s possible using replace all, but what rules would achieve this?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can map the function
(Boole[2 == #]) &onto the list.Explanation of the different parts:
/@applies a function to each element on the list.() &is the syntax used for anonymous functions, and the parameter the function takes is given the name#.BooleconvertsTrue/Falseto1/0.So, in total, we create an anonymous function, which compares its input to
2, and gives either0or1. This function is then mapped onto the list.