# Tagged Questions

Questions about Mathematica's rule-based replacement functionality, including functions such as Rule (->), Replace, ReplaceAll (/.) and ReplaceRepeated (//.).

2answers
184 views

### Replace expression, but only “outside” of a function [duplicate]

I have a horrid expression, which is basically a sum of exponents with coefficients. Here's a taste: What I want to do is to replace ks with another expression, ...
2answers
199 views

### How can you resize all the images in a notebook section?

I've pasted a large number of photos and they are too big which makes scrolling hard. Is there a way to automatically have Mathematica size them to 10% or something reasonable without have to click on ...
0answers
86 views

### How to substitute inside Hold? How to create table of summation (with Sigma symbol) expressions? [duplicate]

I would like to create table of summation expressions, which are not evaluated, but summation limits are substituted. I can't accomplish this so far. If I write I get summation expanded. If I ...
0answers
141 views

### Speed up replacement using Rule Dispatch Associtation

I knew that this has asked many times, but I hope there is something more could be done to speed this up. The expression kappa is here. ...
2answers
125 views

### Why does Rule behave like this?

Why does {x, x^2, x^3, a, b} /. x^n_ -> f[n] result in {x, f[2], f[3], a, b} when ...
4answers
236 views

### Question about ReplacePart

This is a question about ReplacePart but if there's a better that's great. If I have a list a= {1,2,3,4,5} and I want to replace elements ...
0answers
84 views

...
3answers
202 views

### How to Update 2D List based on a binary array

I have 3 lists: numbers = {{1, 2}, {3, 4}, {5, 6}}; toUpdate = {{1, 0}, {0, 0}, {1, 1}}; newValues = {{10, 20}, {30, 40}, {50,60}}; I have to update the list <...
1answer
44 views

### Substitution breaking the scope

A novice's problem, perhaps: it seems that /. substitution does weird things to the scope. ...
1answer
89 views

### Return the incomming expression on false

when I have an expression, that is unknown to mathematica, it is returned exactly the same, as it was called. For example SeeFlyingPigs[1,2] called on the line ...
5answers
398 views

### Select a particular rules out of a list

rule = {beta -> 4, alpha -> 2, x -> 4, z -> 2, w -> 0.8} Select[rule, beta] Select[rule, beta &] Select[rule, beta -> _ &] How would I ...
2answers
479 views

### Replace variables and redefine a function

I would like to redefine a function f(x)=1+2x^2 to a new function g(y)=1+2y by using the replacement ...
1answer
87 views

### How to use rules in simplify this expression?

D[1/(1 + Exp[-x]), x] % /. {1/(1 + Exp[-x]) -> p} Suppose I know that the rule can be applied, but Mathematics just won't do it straight away as the expression ...
2answers
200 views

### Derivative of Vectoral NDSolve Interpolated Function / Rule Solutions,/., and Vector NDSolve [duplicate]

I need to find the instantaneous phase and instantaneous frequency for a rotating system. I am having difficulty making the math work with interpolating functions. First, here is a problem without ...
3answers
136 views

### ReplacePart equivalent for Associations

I have a fairly complicated Association. I wish to use/create a function such as ReplacePart to operate on certain Keys within this multi-level Association. Here's ...
3answers
504 views

### Simple image processing very slow

I have compared the speed of some simple image processing routines between Mathematica an IDL. 1. Reading of a grey scale png image of (720,577) pixels: The image is: Mathematica: ...
1answer
233 views

### Better replacement rules to perform this change of variables?

In cosmology, it is common to work with both a normal time variable t and a “conformal” time variable τ, which is defined such ...
4answers
209 views

### ReplaceImageValue and ReplacePixelValue extremely slow

I have a grey scale image: and I am doing the following: bin = Binarize[image, 0.18] ...
4answers
209 views

### How to replace all symbols except several?

I'm doing some heavy symbolic calculations and for preliminary simple tests I need to replace all coefficients except chosen with 1, e.g.: ...
2answers
136 views

### Is Replace doing double work?

In an answer to this question, rm -rf used the function Replace with level spefication to do a series of replacements inside out. As far as I can see, the order of substitutions when more than one ...
1answer
154 views

### Is this a bug in NCAlgebra library?

In course of my work with NCAlgebra package for Mathematica, http://math.ucsd.edu/~ncalg/ , I encountered unexpected behavior of replacements. I am using ...
3answers
563 views

### Replace all integers by 1?

Let list1 be a list of variables of the form {c1 a, c2 b, ...} where $a,b,...\in\mathbb{C}$ and $c1,c2,...\in\mathbb{Z}$. I am ...
1answer
217 views

### replace RHS of rule

is there an elegant way of replacing the RHS of a rule with the value of some function of it. Say I have {{a -> 1.2}, {a -> 2.3}} and I want to replace ...
3answers
113 views

### How to select the value of a substitution rule for a particular variable?

Say I have the following lists of rules: ...
2answers
294 views

### Why Infinity /. _?(# < 6 &) -> 0 replaces Infinity with ComplexInfinity?

Suppose I define a matrix: mat = { {Infinity, 4, 9}, {6, Infinity, 10}, {8, 9, Infinity} }; Now I want to replace all values smaller than ...
3answers
355 views

### Replace x, but not x[a] [duplicate]

I want to replace all occurences of the symbol x with x[a], as long as it is not in the form ...
1answer
264 views

### Why does the replace all (/.) evaluate differently in plot

I'm plotting this function: Plot[(2 (l/2 π)^2 ((2 π f)/c)^2 /. {f -> (f 10^9), l -> .01, c -> 3 10^8}), {f, 17.5, 26.5}] but it does not plot ...
3answers
218 views

### A rule replacement problem: list -> list

Suppose I have a vector function f[x_] := {x[[1]]^2 - x[[2]]^2, Sin[x[[1]]] + Cos[x[[2]]]}; Then I want to make a list of gradients at some values: ...
2answers
133 views

### replace two elements in a list with the sum of those two elements?

I have a list: G[3] = {2, 4, 2, 4, 2, 4, 2, 4, 2, 4} (there's a 3 in there for the 3rd Prime) Recursively defined by: ...
2answers
178 views

### Remove redundant parameters from equation

I have random expressions like (b1 x + b2 x)/(b3 + b4/b5 x) + Sin[b6 x] where $x$ is a variable and {b1, ..., b6} are parameters to be fitted. This expression is ...
3answers
174 views

### Randomly changing matrix entries

Write a block of code that generates a 4x4 matrix with randomized real number entries each between 0 & 1, then selects 3 matrix entries at random, and finally replaces these 3 entries with a "...
0answers
103 views

### Understanding the order of applying Rule to expression [closed]

I am a little confused about: In what order is a Rule tried to apply to an expression? In a depth-first order or otherwise? For example, ...
0answers
154 views

### Replace part in a matrix that matches specific pattern

I have the following problem. Given a matrix L which looks as follows, Matrix L is a link, route matrix. Further, I got the following vector (d), which indicates the demand per vertex combination. ...
2answers
250 views

### Replace list of symbols in an expression

I have an expression: exp = a+b+c and a list containing all the symbols of my expression: syms = {a,b,c} and I would like ...
2answers
308 views

### Apply Trig Identity to Transform Sum of Sinusoids into Phase-Shifted Cosine Function

I'm trying to solve for the output of a simple RC low-pass filter, with Cos[w t] input. The convolution of the input with the RC transfer function gives me the right output, but I'd like to be able to ...
2answers
66 views

### replace an interval of values [duplicate]

I want to replace values in an interval [a,b] by 0 in a list of values. Example: {{.5,.2,.49,1,1},{.49,.5,.495,1}} I want to get: ...
4answers
272 views

### How to Remove Undirected Edges that are the Same [duplicate]

Given: theData = {1 <-> 2, 2 <-> 3, 2 <-> 1} I would like to remove any duplicates where I define a duplicate to be: ...
3answers
85 views

### How to get an element of a list , which is computed out of substituted variables?

What is the best way to get the element of a list, which is computed out of substituded variables? For example one wants to get the first element of the first row of ...
2answers
88 views

### Conditional replace of a column

Im trying to replace the value in the Nth column of a list if a condition is true, returning the modified list. I've tried modifying the answer from 61219 and been through most of related questions ...
1answer
246 views

### Conditional string replacement

I have a list of text and need to replace an element based on the text that appears in the element before it. Example of my list: ...
1answer
205 views

### Simplifying a b = 1 in non-commutative products?

Yesterday, I try to define some rules for non-communicative multiplication, here. I find some quite helpful rules, but things still do not work properly; the distributive rule can't be applied (e.g.: <...
6answers
197 views

### Change last column of a matrix

Given a (rectangular) matrix of integers, i.e. RandomInteger[4, {10, 3}] I want to apply the following rules only to the last column ...
0answers
42 views

### How to do replacements in Compile'd code? [duplicate]

I want to do something in the spirit of this code: ...
3answers
439 views

### How to replace the first occurance in an expression instead of replacing all?

Is there a function that replaces the first occurance of the expression instead of replacing all? For example, if I have HoldForm[x + 2 + 4 + x] /. x -> 4, is ...
7answers
1k views

### How to assign a list of numbers into letter grades

Here is my list: ...
3answers
2k views

### Assign output of solve to variables [duplicate]

I have this input: Solve[x^2 + 3 x + 2 == 0, x] which gives this output: {{x -> -2}, {x -> -1}} I want the first x ...
1answer
97 views

### Function returned an unexpected list

I was trying out a question in Wellin's Programming with Mathematica that says the following. Question: Given a set of points in the plane (or 3-space), find the maximum distance between any pair of ...
1answer
96 views

### Select a Placeholder in a Notebook?

How might I select the 1st Placeholder in a Notebook? SelectionMove Has no option. ...
1answer
87 views

### Add to the last digit a number

I have data from a Multimeter that has a precision of 0.6%rdg+4dgt. So i want to take the last digit of, say, 12.007 and add 4 to the last digit like ...
1answer
66 views

### Speeding up ReplaceRepeated while truncating to desired order

I need to program in an algorithm that recursively makes algebraic replacements which leads to an utterly complicated algebraic function of $x$, but whose final result is only needed at fixed order in ...