Mathematica Mathematica: If statment for lists

  • Thread starter Thread starter Sarah rob
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
To create an If statement that processes two lists, the goal is to check if elements in list1 are greater than zero. If true, the corresponding element from list2 is subtracted; if false, the original value from list1 is retained. The code provided demonstrates this logic effectively. By defining the function f that takes two arguments and using MapThread to apply it across the lists, the output is successfully generated as {0, 2, 5, 4}. This approach simplifies the representation of the elements in the lists, allowing for clear input and output.
Sarah rob
Messages
16
Reaction score
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
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}
 

Similar threads

Replies
2
Views
956
Replies
4
Views
2K
Replies
1
Views
3K
Replies
1
Views
3K
Replies
2
Views
2K
Replies
3
Views
3K
Back
Top