Mathematica: If statment for lists

  • Context: Mathematica 
  • Thread starter Thread starter Sarah rob
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

The discussion focuses on creating an If statement in Mathematica to manipulate two lists, list1 and list2. The goal is to subtract elements of list2 from list1 only when the corresponding elements in list1 are greater than zero. The provided solution utilizes the MapThread function along with a custom function f to achieve the desired output of {0, 2, 5, 4}. This method effectively demonstrates how to apply conditional logic to list elements in Mathematica.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of list manipulation in Mathematica
  • Basic knowledge of conditional statements in programming
  • Experience with functional programming concepts, particularly mapping functions
NEXT STEPS
  • Explore advanced list manipulation techniques in Mathematica
  • Learn about the use of Map and MapThread functions in Mathematica
  • Investigate other conditional functions in Mathematica, such as Which and Switch
  • Study performance optimization for large lists in Mathematica
USEFUL FOR

Mathematica users, data analysts, and programmers looking to enhance their skills in list processing and conditional logic within the Mathematica environment.

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 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K