New answers tagged

1 vote

Selecting patterns

Using Lookup: ...
Syed's user avatar
  • 49.3k
3 votes

Cases in list with varying length in sublists

...
E. Chan-López's user avatar
2 votes

Cases in list with varying length in sublists

...
eldo's user avatar
  • 62.2k
0 votes

Problem with WordBoundary in string patterns

To steal examples from the page: ...
Syed's user avatar
  • 49.3k
2 votes

Highlight elements in the list using pattern matching

list = {{a, b, c}, {d, e, f}, {g, h, i}, {b, c, d}, {c, a, m}, {c, d, n}}; sub = Subsets[list, {2}]; Another variant of @kglr's solution using ...
E. Chan-López's user avatar
1 vote

Highlight elements in the list using pattern matching

list = {{a, b, c}, {d, e, f}, {g, h, i}, {b, c, d}, {c, a, m}, {c, d, n}}; sub = Subsets[list, {2}]; Using ...
eldo's user avatar
  • 62.2k
0 votes

How to pass arguments of outer function into inner function implicitly?

I really think this is a bad idea, but if you truly want "implicit", you may need to let your special symbols live "outside" your functions. You could do this by setting up a ...
lericr's user avatar
  • 26.6k
1 vote

How to pass arguments of outer function into inner function implicitly?

I feel like I must be missing something, because it sounds like you just want a straightforward passing of arguments: ...
lericr's user avatar
  • 26.6k
1 vote

Pattern matching with DeleteCases

String matching is different than general pattern matching. Let's look at the full form of the expressions: ...
lericr's user avatar
  • 26.6k
1 vote

Combine list elements preceding a specific element

...
E. Chan-López's user avatar
1 vote

Combine list elements preceding a specific element

...
eldo's user avatar
  • 62.2k
1 vote

How to match a pattern only when it is not used as a head?

expr = -27 f + 27 f[1 + n] - 9 f[2 + n] + f[3 + n] == 1 + 3 n; Another way using Cases: ...
E. Chan-López's user avatar
12 votes

How to match a pattern only when it is not used as a head?

This trick is often useful with ReplaceAll: if you want some things to remain unaffected, you add a trivial replacement rule for that case before the replacement ...
Sjoerd Smit's user avatar
  • 23.1k
5 votes

How to match a pattern only when it is not used as a head?

expr = -27 f + 27 f[1 + n] - 9 f[2 + n] + f[3 + n] == 1 + 3 n expr /. f[a__] :> g[a] /. f -> f[n] /. g[a__] :> f[a] Result: -27 f[n] + 27 f[1 + n] - 9 ...
Syed's user avatar
  • 49.3k
8 votes
Accepted

How to match a pattern only when it is not used as a head?

expr = -27 f + 27 f[1 + n] - 9 f[2 + n] + f[3 + n] == 1 + 3 n; p = Position[expr, f, Heads -> False] {{1, 1, 2}} ...
eldo's user avatar
  • 62.2k
10 votes

What's the difference between With and ReplaceAll?

The way With[{x=x0, …}, body] works is to take body, and replace every occurrence of x, etc. ...
xzczd's user avatar
  • 65.6k
11 votes

What's the difference between With and ReplaceAll?

With is a scoping construct, meaning that symbols localized by With will be unaffected by whatever value they may have outside ...
Sjoerd Smit's user avatar
  • 23.1k
1 vote
Accepted

Matrix Substitution & Operator Adjustment

Inspired by @march's insightful contribution, we can now tackle even more complex examples like this one: {{a + b f x y^3 z + c, e x}, d x y z^2, x}}. As you'll see,...
ssskkkky's user avatar
1 vote

Trouble with expressing "anything" using patterns?

You can use ReplaceAll and named Pattern with RuleDelayed if you are not too concerned with ...
Edmund's user avatar
  • 42.1k
1 vote

How can I enforce the use of Subtract and Divide?

Mathematica provides no way to modify the parser. Therefore the choice between, for example, writing 'a/b' and 'Divide[a,b]' is mutually exclusive, as the former will always be parsed as 'Times[a, ...
Glenn Welch's user avatar
1 vote

How can I enforce the use of Subtract and Divide?

The Mathematica kernel machinery relies in the first place on pattern matching. The problem of coding Mathematica is not time but unique identification of terms and lookup tables for typical ...
Roland F's user avatar
  • 3,308
3 votes

Trouble with expressing "anything" using patterns?

Here are a couple of way, but be warned that we'll need to fix some ambiguities in your question and we'll need to determine whether you really need mutation side-effects: ...
lericr's user avatar
  • 26.6k
5 votes

Cancelling factor in fractions with factorial

There is a way to do it without replacement rules in the cases discussed here. Consider exp1 = k/k!; exp2 = ((k (1 - x)^(-k + n) x^(-1 + k) n!)/(k! (-k + n)!)); ...
bmf's user avatar
  • 14.9k
2 votes

DeleteCases depending on all elements

l = {{10, 5}, {20, 4}, {20, 3}, {10, 6}}; Using the third argument of GroupBy: ...
E. Chan-López's user avatar
2 votes

DeleteCases depending on all elements

f = Last /@ DeleteCases[{{__}}] @ GatherBy[Sort @ #, First]&; list like in question ...
eldo's user avatar
  • 62.2k
1 vote

How to apply Cases on multiple parts of sublists?

list = {{2063, 853}, {2066, 855}, {2069, 857}, {2072, 859}}; Using SequenceSplit (new in 11.3) ...
eldo's user avatar
  • 62.2k
5 votes

Apply SequenceReplace with a fixed shift

Using SequenceReplace: Make one change at a time and wrap it in FixedPoint. To see all the intermediate outputs, use ...
Syed's user avatar
  • 49.3k
2 votes

Apply SequenceReplace with a fixed shift

An interesting problem. We cannot use SequenceReplace here because, unlike SequenceCases, it doesn't have an ...
eldo's user avatar
  • 62.2k
0 votes

Delete sublist when sub-sublists contain same element

Using DisjointQ: ...
Syed's user avatar
  • 49.3k
1 vote

Delete sublist when sub-sublists contain same element

list = {{{a}, {b, c, d}}, {{a, b}, {b, c}}, {{b, c}, {a, b, c}}, {{b}, {d}}, {{a, b}, {c, d}}}; Using function deconstruction ...
eldo's user avatar
  • 62.2k
1 vote

Replace elements of a list if the conditional is false

Clear["Global`"]; lis = {1, {a, b, c}, 1, {1, 2, 3, 4, 5, 6, 7, 8}, {A, B, D, Z}} # # === # & /@ lis // Boole or ...
Syed's user avatar
  • 49.3k
1 vote

Joining adjacent elements by rule

Using SplitBy: ...
Syed's user avatar
  • 49.3k
1 vote

Joining adjacent elements by rule

...
E. Chan-López's user avatar
1 vote

Replace elements of a list if the conditional is false

list = {1, {a, b, c}, 1, {1, 2, 3, 4, 5, 6, 7, 8}, {A, B, D, Z}}; Using ReplacePart: ...
E. Chan-López's user avatar
2 votes

Joining adjacent elements by rule

Using kglr's lists ...
eldo's user avatar
  • 62.2k
1 vote

Replace elements of a list if the conditional is false

list = {1, {a, b, c}, 1, {1, 2, 3, 4, 5, 6, 7, 8}, {A, B, D, Z}}; Using SequenceSplit (new in 11.3) ...
eldo's user avatar
  • 62.2k
0 votes

Replace zeros in a list with last nonzero value

Looking for a mostly arithmetic solution, I found a function that replaces only the first zero: f[list_] := list + Prepend[ Rest[1 - Unitize[list]] Most[list], 0] ...
Vito Vanin's user avatar
2 votes

How can I delete rows from a matrix?

list = {{-0.01, .4}, {-0.47, -0.054}, {-0.82, -0.07}, {-1.55, -0.08}, {0.10 + 3.14 I, -0.03}, {-0.54, -0.051}, {0.03, 0.10 + 3.14 I}}; Using ...
E. Chan-López's user avatar
1 vote

How to replace numbers by letters in a special way?

Using ArrayRules and SparseArray: ...
E. Chan-López's user avatar
2 votes

How can I delete rows from a matrix?

list = {{-0.01, .4}, {-0.47, -0.054}, {-0.82, -0.07}, {-1.55, -0.08}, {0.10 + 3.14 I, -0.03}, {-0.54, -0.051}, {0.03, 0.10 + 3.14 I}}; Using ...
eldo's user avatar
  • 62.2k
1 vote

How to replace numbers by letters in a special way?

list = {{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}}; Using Threaded (new in 13.1)...
eldo's user avatar
  • 62.2k
2 votes

Replace zeros in a list with last nonzero value

list = {-1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1}; Using SplitBy, SequenceCases and ...
E. Chan-López's user avatar
3 votes

Replace zeros in a list with last nonzero value

list = {-1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1}; Using SequenceSplit (new in 11.3) ...
eldo's user avatar
  • 62.2k
1 vote

Removing all sublists containing any arbitrary string as one or more of its list component

...
E. Chan-López's user avatar
1 vote

Removing all sublists containing any arbitrary string as one or more of its list component

...
eldo's user avatar
  • 62.2k
2 votes

The problem of matching consecutive cross-line strings

...
eldo's user avatar
  • 62.2k
5 votes

How to convert this logarithm into this form?

expr = Log[Log[2]]/Log[2]; expr /. Log[x_]/Log[a_] :> Defer[Log[a, x]] Log[2, Log[2]]
Syed's user avatar
  • 49.3k
9 votes
Accepted

How to convert this logarithm into this form?

Using the property ...
Nasser's user avatar
  • 142k
3 votes
Accepted

The problem of matching consecutive cross-line strings

...
xzczd's user avatar
  • 65.6k
4 votes

The problem of matching consecutive cross-line strings

Lets suppose we have this fake data generator ...
Gustavo Delfino's user avatar

Top 50 recent answers are included