Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica Pattern matching and replace repeated

  1. Apr 22, 2012 #1
    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.
  2. jcsd
  3. Apr 22, 2012 #2
    Here is one version (without rules):

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

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

    Code (Text):
    {3, 5, 6, 8, 9}
  4. Apr 22, 2012 #3
    Okay thanks
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Similar Threads for Mathematica Pattern matching Date
Mathematica Storing Mathematica output Mar 30, 2018
Mathematica Mathematica to MATLAB Mar 25, 2018
Mathematica Cannot do the integral of the Hyper-geometric function? Mar 11, 2018
Mathematica Cannot Plot This Function in Mathematica Feb 26, 2018
Basic Mathematica pattern matching May 16, 2012