Mathematica Pattern matching and replace repeated

  • Mathematica
  • Thread starter 6.28318531
  • Start date
  • Tags
    Mathematica
I'll try that.In summary, a rule can be created using pattern matching and //.to generate an ordered list of integers that are larger than the previous numbers in the list. A function called larger can be used to check pairs of numbers and discard one if it is less than the other. The syntax for this function is Select[Transpose[{lst, Prepend[Most[lst], lst[[1]] - 1]}], #[[1]] > #[[2]] &][[All, 1]].
  • #1
6.28318531
54
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
  • #2
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}
 
  • #3
Okay thanks
 

Related to Mathematica Pattern matching and replace repeated

What is pattern matching in Mathematica?

Pattern matching in Mathematica is a powerful tool for finding and manipulating specific patterns within data. It allows you to search for and replace specific structures, expressions, and patterns within your data, making it a useful tool for data analysis and manipulation.

How does pattern matching work in Mathematica?

Pattern matching in Mathematica works by using a set of rules to identify patterns within your data. These rules can be simple patterns, such as matching specific values or structures, or more complex patterns, such as using wildcards and variables to match a range of data. When a pattern is found, it can be replaced with a desired expression or structure.

What is repeated pattern matching in Mathematica?

Repeated pattern matching in Mathematica allows you to find and manipulate patterns that occur multiple times within your data. This is useful for tasks such as finding and replacing all instances of a specific expression in a larger dataset.

What is the difference between Replace and ReplaceRepeated in Mathematica?

The Replace function in Mathematica replaces the first instance of a pattern in your data, while the ReplaceRepeated function replaces all instances of a pattern. This means that ReplaceRepeated is useful for tasks such as replacing all repeated patterns, while Replace may only replace the first instance.

Can you use pattern matching in Mathematica with more complex data types?

Yes, pattern matching in Mathematica is not limited to simple expressions and structures. It can be used with more complex data types, such as lists, matrices, and even custom-defined data types. This makes it a versatile tool for data manipulation and analysis in various fields of science and research.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
5K
Replies
9
Views
1K
Replies
64
Views
2K
Replies
14
Views
1K
Replies
0
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Atomic and Condensed Matter
Replies
3
Views
2K
Back
Top