Mathematica: If statment for lists

  • Mathematica
  • Thread starter Sarah rob
  • Start date
  • Tags
    Mathematica
In summary, to use an "If" statement for lists in Mathematica, you can use the "Map" function to apply it to each element. Multiple conditions can be used by using the "And" or "Or" functions. To apply it to only a portion of a list, you can use the "Take" function. The "ReplacePart" function allows for in-place modification using an "If" statement. Lastly, the "Select" function can be used to filter a list based on the condition in the "If" statement.
  • #1
Sarah rob
16
0
I want to create an If statement that states; if the elements in, list1>0 , true subtract list2, false keep value from list1 so that i
end up with ans: {0,2,5,4}
list1 = {0, 2, 8, 9}
list2 = {1, 0, 3, 5}
I don't know how to represent the elements in the lists so that my If statement works
 
Physics news on Phys.org
  • #2
Excellent example showing input and output. That makes it much easier to provide correct help.

In[12]:=list1={0,2,8,9};list2={1,0,3,5};
f[x_,y_]:=If[x>0,x-y,x];
MapThread[f,{list1,list2}]

Out[15]={0,2,5,4}
 

Related to Mathematica: If statment for lists

1. How do I use an "If" statement for lists in Mathematica?

To use an "If" statement for lists in Mathematica, you can use the "Map" function. First, define the list you want to apply the "If" statement to. Then, use the "Map" function to apply the "If" statement to each element of the list. This will create a new list with the results of the "If" statement applied to each element.

2. Can I use multiple conditions in an "If" statement for lists in Mathematica?

Yes, you can use multiple conditions in an "If" statement for lists in Mathematica by using the "And" or "Or" functions. These will evaluate the conditions and return a boolean value, which can then be used in the "If" statement.

3. How do I apply an "If" statement to only a portion of a list in Mathematica?

To apply an "If" statement to only a portion of a list in Mathematica, you can use the "Take" function to specify the elements you want to apply the statement to. Then, use the "Map" function to apply the "If" statement to the selected elements, creating a new list with the results.

4. Is there a way to use an "If" statement to modify a list in-place in Mathematica?

Yes, you can use the "ReplacePart" function to modify a list in-place using an "If" statement. This function allows you to specify the position of the element you want to modify and the value you want to replace it with, based on the condition in the "If" statement.

5. Can I use an "If" statement to filter a list in Mathematica?

Yes, you can use an "If" statement to filter a list in Mathematica by using the "Select" function. This function will evaluate the condition for each element in the list and only return the elements that meet the condition, creating a new filtered list.

Similar threads

  • Programming and Computer Science
Replies
2
Views
673
  • Programming and Computer Science
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
1K
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
364
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Replies
4
Views
2K
Back
Top