Mathematica Pattern matching and replace repeated

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

This discussion focuses on using Mathematica's pattern matching and replacement functionality to filter a list of integers, retaining only those that are larger than their predecessors. The user seeks to implement a rule using the syntax `L //. rules` to achieve this, with an example list provided: {3, 5, 2, 0, 6, 1, 8, 4, 9}. The proposed solution involves defining a function `larger[lst_]` that utilizes `Select` and `Transpose` to compare adjacent elements, successfully returning the desired output: {3, 5, 6, 8, 9}.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of pattern matching in Mathematica
  • Knowledge of list manipulation techniques in Mathematica
  • Basic programming concepts related to functions and conditionals
NEXT STEPS
  • Explore advanced pattern matching techniques in Mathematica
  • Learn about the `Select` function and its applications in list filtering
  • Investigate the use of `Transpose` for pairing elements in lists
  • Study the implementation of custom rules in Mathematica for data transformation
USEFUL FOR

This discussion is beneficial for Mathematica users, data analysts, and programmers interested in list processing and pattern matching techniques within the Mathematica environment.

6.28318531
Messages
51
Reaction score
0
I decided to ask here as well, because maybe someone will see it. Given a list of integers, use pattern matching and //.to create a rule that generates an ordered list of integers that are larger than the previous numbers in the list, eg given {3,5,2,0,6,1,8,4,9}, you should get {3,5,6,8,9}.I tried something like rules = {x_,y_,__}/;x>y -> {x,___}, calling our list L, L//.rules . I haven't really used this function before, what is the correct syntax? I want to check pairs of numbers and discard one if its less than the other. Any help would be appreciated.
 
Physics news on Phys.org
Here is one version (without rules):

Code:
larger[lst_] := Select[Transpose[{lst, Prepend[Most[lst], lst[[1]] - 1]}], #[[1]] > #[[2]] &][[All, 1]]

e.g.

Code:
larger[{3, 5, 2, 0, 6, 1, 8, 4, 9}]

gives:

Code:
{3, 5, 6, 8, 9}
 
Okay thanks
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K