New answers tagged

1 vote

Indexed replacement

expr = 3 a + 4 b + 21 c; Plus @@ Times @@@ Transpose[{Range @ Length @ expr^2, Cases[expr, _Symbol, {2}]}] a + 4 b + 9 c
eldo's user avatar
  • 60.7k
1 vote

Conditionally replace rows in lists

A variant of E. Chan-López positional solution ...
eldo's user avatar
  • 60.7k
2 votes

Replace multiple elements at different positions

a = {0, 0, 0, 0, 0}; p = {1, 4, 5}; Using SubsetMap: ...
E. Chan-López's user avatar
2 votes

Conditionally replace rows in lists

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

Indexed replacement

For your first request, we can use ReplacePart as follows: ...
E. Chan-López's user avatar
1 vote

Applying a function to every item in a column of a matrix

SeedRandom[0]; list = RandomInteger[10, {3, 4}]; Grabbing the @eldo's list and playing with ReplacePart, we can apply one ...
E. Chan-López's user avatar
2 votes

Applying a function to every item in a column of a matrix

Using Fold: ...
Syed's user avatar
  • 48.6k
2 votes

Applying a function to every item in a column of a matrix

SeedRandom[0]; list = RandomInteger[10, {3, 4}]; One function applied to two columns ...
eldo's user avatar
  • 60.7k
1 vote

How do I replace specific sequences of a sublist without changing the level of the list?

list = {{1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1}, {1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0}}; Using ReplaceList: ...
E. Chan-López's user avatar
1 vote

How do I replace specific sequences of a sublist without changing the level of the list?

list = {{1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1}, {1, 0, 0, 1, 1, 1, 1, 0,1, 1, 1, 0}}; Using SequenceCases with ...
eldo's user avatar
  • 60.7k
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.4k
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
  • 22.9k
7 votes
Accepted

How to correctly replace scalars with matrices?

"MatrixFunction" will do what you want. Here is an example with "Sqrt": ...
Daniel Huber's user avatar
  • 49.9k
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

How to replace one part of list of lists

Sjoerd Smit has posted a very neat method for applying a function to a matrix column: ...
user1066's user avatar
  • 16.8k
2 votes

How to replace one part of list of lists

data = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}}; Using Distribute: ...
E. Chan-López's user avatar
2 votes

How to replace one part of list of lists

list = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}}; Showing some of the newer functions ...
eldo's user avatar
  • 60.7k
3 votes

Replacing parts of a matrix with lists

a = {{1, 0}, {0, 1}}; b = {13, 14}; Using SubsetMap: ...
E. Chan-López's user avatar
2 votes

Replacing parts of a matrix with lists

a = {{1, 0}, {0, 1}}; b = {13, 14}; I suggest ReplaceAt (new in 13.1) or MapAt ...
eldo's user avatar
  • 60.7k
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
1 vote

Why can't I assign upvalues to NonCommutativeMultiply?

How about something like this? ...
Sjoerd Smit's user avatar
  • 22.9k
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
  • 60.7k
1 vote
Accepted

How to check a group of solutions of partial differential equations?

I mean like this ...
Nasser's user avatar
  • 142k
1 vote
Accepted

How to make the following replacement?

You can explicitly check for the presence of arguments with ContainsAny. ...
Domen's user avatar
  • 21.9k
1 vote
Accepted

Animate a FindMinimum with NIntegrate

As a higher-level comment, most programming languages use "functions" as building blocks. You explicitly specify inputs and outputs. Mathematica allows you to use "expressions" ...
Yaroslav Bulatov's user avatar
2 votes
Accepted

Converting trigonometric expressions between sine and cosine forms

Perhaps: ...
Goofy's user avatar
  • 2,582
2 votes
Accepted

Replacement rule for unevaluated LaplaceTransform

Perhaps something like the following will work for you? ...
Carl Woll's user avatar
  • 130k
1 vote

How do I get Replace to work with a complex repeating pattern?

list = {"*A", "B", "*C", "D", "E", "F", "*G", "H", "*I"}; Using ...
E. Chan-López's user avatar
4 votes

How do I get Replace to work with a complex repeating pattern?

list = {"*A", "B", "*C", "D", "E", "F", "*G", "H", "*I"}; Build and query ...
eldo's user avatar
  • 60.7k
4 votes
Accepted

How do I get Replace to work with a complex repeating pattern?

SequenceCases[list, {"*C", x__} /; Nor @@ StringStartsQ[{x}, "*"] :> {x}] ...
kglr's user avatar
  • 392k

Top 50 recent answers are included