Using an If loop in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter Physicslad78
  • Start date Start date
  • Tags Tags
    Loop Mathematica
Click For Summary

Discussion Overview

The discussion revolves around using loops in Mathematica, specifically focusing on eliminating pairs from a list based on a condition and manipulating numbers based on their parity. Participants explore different methods to achieve these tasks, including the use of If statements and built-in functions.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant seeks help to eliminate pairs from a list where the second element exceeds 1, suggesting the use of an If loop.
  • Another participant provides a solution using a Do loop with an If statement to append qualifying pairs to a new list.
  • A different participant poses a question about printing half of a number if it is even and twice the number if it is odd, indicating uncertainty about how to implement this using loops.
  • A participant shares a method using the DeleteCases function to eliminate pairs based on a condition, demonstrating a built-in function approach.
  • Another method is presented using a For loop to print numbers based on their parity, showcasing an alternative looping structure.
  • Lastly, a participant illustrates a method using pattern matching to achieve the same result without explicit loops, indicating the flexibility of Mathematica's capabilities.

Areas of Agreement / Disagreement

Participants present multiple approaches to the same problem, indicating a lack of consensus on the best method. There are competing views on whether to use loops or built-in functions, and the discussion remains open-ended regarding the most efficient solution.

Contextual Notes

Some methods rely on specific Mathematica functions and may not address all edge cases or assumptions about the input data structure. The effectiveness of each approach may depend on the context in which it is applied.

Who May Find This Useful

Individuals interested in programming with Mathematica, particularly those looking to understand different looping constructs and built-in functions for list manipulation and conditional operations.

Physicslad78
Messages
46
Reaction score
0
Dear all,

I have a list of pairs say Li={{a,x},{b,y},{c,z}...}. I want to eliminate from this list all pairs that have their second part i.e the x, y, z...>1 and I was thing of using an If loop. Can anyone tell me how please...



Thanks...
 
Physics news on Phys.org
There is probably a more elegant way of doing it (it wouldn't surprise me if Mathematica has some built in function for this), but this should get you started:

Code:
L2 = {};
Do[
    If[Li[[i, 2]] <= 1, AppendTo[L2, Li[[i]]]]
    ,{i, Length[Li]}]
L1 = L2;
 
Last edited:
question : For each number k from 1 to 10, print half the number if k is even and twice the number if k is odd.
<I think this question involving loop but i ddnt how to solve it. anyone ?>
 
(*One built in function to eliminate pairs matching a condition using pattern matching*)

In[1]:= v={{1,7},{2,-5},{3,0},{4,9}};DeleteCases[v,{x_,y_/;y>1}]
Out[2]= {{2,-5},{3,0}}

(*one method of printing using a For*)

In[3]:= For[k=1,k≤10,k++,
If[EvenQ[k],Print[k/2],Print[2k]]
]

From In[3]:= 2
From In[3]:= 1
From In[3]:= 6
From In[3]:= 2
From In[3]:= 10
From In[3]:= 3
From In[3]:= 14
From In[3]:= 4
From In[3]:= 18
From In[3]:= 5

(*another method using pattern matching*)

In[4]:= k=.; (*clear that value assigned to k*)
Range[10]/.{k_/;EvenQ[k]->k/2,k_/;OddQ[k]->2k}

Out[5]= {2,1,6,2,10,3,14,4,18,5}
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 13 ·
Replies
13
Views
3K