Skip to main content

New answers tagged

2 votes

Why does Replace function not work the first time I try it?

The problem is that the expression doesn't have g[x]/x just isolated from Cos[x]: ...
Sjoerd Smit's user avatar
2 votes

Why does Replace function not work the first time I try it?

Three errors: Execute cells separately if you refer its result. Use :> instead of -> to avoid evaluation of rules already at input time. Don't use %, it was created for the math.exe text input. ...
Roland F's user avatar
  • 3,947
2 votes

Replace items of list except items that are member of another list

a = Transpose[{Range[10], Range[10, 1, -1]}]; b = {{4, 7}, {9, 2}, {2, 9}, {5, 6}}; Using ReplaceAllOutside by Richard ...
eldo's user avatar
  • 81.2k
3 votes

Ignore higher order terms in an expression

expr = α β + α^2 + β^2 + α β^2/2 + α^2 β; vars = {α, β}; Another way is to use PolynomialDegree by Dennis M Schneider and ...
E. Chan-López's user avatar
6 votes

Ignore higher order terms in an expression

Assuming the parts of the expression will consist of things that look like $\alpha ^i \beta^j$ for non-negative $i , j $ We can also use Select: ...
ydd's user avatar
  • 4,587
4 votes
Accepted

Ignore higher order terms in an expression

Rules do not understand mathematics, they understand structures of expressions. So, without help, they don't understand that in $\alpha^2\beta$ you can read it as $\alpha^2\beta^1$, and therefore ...
evanb's user avatar
  • 6,246
3 votes

Ignore higher order terms in an expression

If \[Alpha], \[Beta] are small of equal order try ...
Ulrich Neumann's user avatar
3 votes

Taking the square root of the first element of a list

list = {{4, 1}, {9, 2}, {16, 3}, {25, 4}}; Using ColumnMap by Michael Sollami CMap = ResourceFunction["ColumnMap"]; ...
eldo's user avatar
  • 81.2k
2 votes

Taking the square root of the first element of a list

Query[All, {1 -> Sqrt}]@lst (* {{2, 1}, {3, 2}, {4, 3}, {5, 4}} *) For 'in place' modification: ...
user1066's user avatar
  • 20.8k
3 votes

Taking the square root of the first element of a list

lst = {{4, 1}, {9, 2}, {16, 3}, {25, 4}}; Using MapThread: ...
E. Chan-López's user avatar
4 votes

Taking the square root of the first element of a list

list = {{4, 1}, {9, 2}, {16, 3}, {25, 4}}; NewList = list /. {x_, y_} :> {Sqrt[x], y}
Reda.Kebbaj's user avatar
2 votes

Replace multiple elements in a list

s = {1, 5, 7, 8, 11, 10, 3}; z = {1, 3, 5}; Using MapIf by Taliesin Beynon: ...
E. Chan-López's user avatar
2 votes

Replace multiple elements in a list

list = {1, 5, 7, 8, 11, 10, 3}; z = {1, 3, 5}; 1. Using MapCases by Richard Hennigan (Wolfram Research) ...
eldo's user avatar
  • 81.2k
2 votes

Function to replace values of elements of matrix with 0 unless the value is -1

For something different (an example that can be generalized): The test matrix: mat = RandomInteger[{-4, 4}, {10, 6}]; The approach: ...
ubpdqn's user avatar
  • 62.8k
1 vote

Function to replace values of elements of matrix with 0 unless the value is -1

SeedRandom[1]; m = RandomInteger[{-3, 3}, {4, 7}]; Using SubsetMap: ...
E. Chan-López's user avatar
2 votes

Function to replace values of elements of matrix with 0 unless the value is -1

SeedRandom[1]; m = RandomInteger[{-3, 3}, {4, 7}]; Using MapIf by Taliesin Beynon ...
eldo's user avatar
  • 81.2k
1 vote

Delete the sublist which can be others' subsets

DeleteSublists[list_] := With[{p = Permutations[list, {2}]}, DeleteCases[ list, Alternatives @@ Pick[p, Apply[SubsetQ] /@ p][[All, 2]]]] ...
eldo's user avatar
  • 81.2k
0 votes

MatchQ returns False for a match for e1 ** e2 while matching both e1 and e2

Using RealValuedNumberQ (new in 13.3) MatchQ[(2 e1) ** (-2 e2), (k1_?RealValuedNumberQ x_) ** (k2_?RealValuedNumberQ y_)] True ...
eldo's user avatar
  • 81.2k
2 votes

Replace a column/row of a matrix under a condition by a random number

m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Using Cases ...
eldo's user avatar
  • 81.2k
2 votes

Replace pattern with the number of matches so far

More alternatives that avoid an extra counter variable... inp = {x, y, z, x, y, x, x, z}; Let MapIndexed manage the state after ...
lericr's user avatar
  • 30.4k
3 votes

Replace last element of a list if there is somewhere a 0 in the list

If all members list numbers: ...
ubpdqn's user avatar
  • 62.8k
3 votes

Replace pattern with the number of matches so far

...
ubpdqn's user avatar
  • 62.8k
4 votes

Replace pattern with the number of matches so far

inp = {x, y, z, x, y, x, x, z}; Using Fold: ...
E. Chan-López's user avatar
3 votes

Replace last element of a list if there is somewhere a 0 in the list

Grabbing the @eldo's list: list = {{5, 6, 0, 8, 5, 9}, {8, 4, 4, 3, 5, 8}, {9, 4, 3, 2, 5, 7}, {1, 2, 3, 0, 0, 3}}; Using ...
E. Chan-López's user avatar
3 votes

Replace last element of a list if there is somewhere a 0 in the list

list = {{5, 6, 0, 8, 5, 9}, {8, 4, 4, 3, 5, 8}, {9, 4, 3, 2, 5, 7}, {1, 2, 3, 0, 0, 3}}; Using MapIf by Taliesin ...
eldo's user avatar
  • 81.2k
6 votes

Replace pattern with the number of matches so far

inp = {x, y, z, x, y, x, x, z}; Using SubsetMap (new in 12.0) ...
eldo's user avatar
  • 81.2k
1 vote

Replace elements that do not match a pattern

mat = {{0, a, b}, {c, 0, d}, {0, 0, e}}; A hash-free variant of E. Chan-López answer using SameAs (new in 13.1) ...
eldo's user avatar
  • 81.2k
1 vote

Replace elements that do not match a pattern

mat = {{0, a, b}, {c, 0, d}, {0, 0, e}}; Using ReplaceList: ...
E. Chan-López's user avatar
2 votes

Replace elements that do not match a pattern

mat = {{0, a, b}, {c, 0, d}, {0, 0, e}}; Using MapCases by Richard Hennigan (Wolfram Research) ...
eldo's user avatar
  • 81.2k
2 votes

How to make ReplaceAll recognize what to be replaced in a situation like this?

A short variant of my first answer rep[f_] := Expand[f] /. a_*_Symbol + _[__] :> a ...
eldo's user avatar
  • 81.2k
1 vote

How to delete a pattern from String?

StringReplace and a regular expression with a negative lookahead ...
user1066's user avatar
  • 20.8k
3 votes

How to delete a pattern from String?

str = "{Aaaa -> a, Bbbb -> b, Cccc -> , Ddddd -> c, Eeeee -> , Fffff -> e}"; Using SequenceCases <...
eldo's user avatar
  • 81.2k
3 votes

Change nested list of symbols to nested list of numbers

symbols = {e4, {e200, {e1, {e11, e23}}}}; Using Replace ...
eldo's user avatar
  • 81.2k
3 votes

How to replace a space by a comma

str = "{1 2 3 4 5 6 7 8}"; Using StringCases with NumberString ...
eldo's user avatar
  • 81.2k
2 votes
Accepted

How to `Collect` where `var` is one expression?

This seemed promising: First@GroebnerBasis[{u - (a - b + c), expr1}, {c, b, a, u}] /. u -> (a - b + c) Until it didn't. Slow thinker. Now this: ...
Michael E2's user avatar
  • 239k

Top 50 recent answers are included