ReplaceAll
Replaces each occurrence of a given value by another value.
Syntax
-
ReplaceAll(X, x, y[, n])
-
X
is a container -
x
is any object -
y
is any object -
n
is a positive integer
-
Description
If X
is a container, x
any object, and y
any object, then ReplaceAll(X, x, y)
returns X
with every instance of x
replaced by y
. ReplaceAll(X, x, y)
replaces elements on level n
, with level n = 1
being X
itself.
Examples
A ≔ RandomIntMatrix(10, 0, 10)
⎛6 2 3 1 9 1 3 3 6 9⎞ ⎜9 2 1 7 3 9 2 2 3 6⎟ ⎜2 9 3 7 7 9 7 2 4 8⎟ ⎜0 2 7 2 9 9 4 7 6 4⎟ ⎜5 6 4 6 3 8 3 4 9 5⎟ ⎜4 0 9 3 5 8 6 9 0 3⎟ ⎜7 3 3 5 4 5 2 9 9 9⎟ ⎜2 2 2 7 8 6 4 7 7 0⎟ ⎜6 0 7 7 7 4 4 7 3 1⎟ ⎝8 4 0 5 1 8 4 2 7 9⎠
ReplaceAll(A, 5, 50)
⎛ 6 2 3 1 9 1 3 3 6 9⎞ ⎜ 9 2 1 7 3 9 2 2 3 6⎟ ⎜ 2 9 3 7 7 9 7 2 4 8⎟ ⎜ 0 2 7 2 9 9 4 7 6 4⎟ ⎜50 6 4 6 3 8 3 4 9 50⎟ ⎜ 4 0 9 3 50 8 6 9 0 3⎟ ⎜ 7 3 3 50 4 50 2 9 9 9⎟ ⎜ 2 2 2 7 8 6 4 7 7 0⎟ ⎜ 6 0 7 7 7 4 4 7 3 1⎟ ⎝ 8 4 0 50 1 8 4 2 7 9⎠
compute(RandomIntMatrix(4, 1, 10), n, 1, 5)
⎛7 8 4 7⎞ ⎛7 6 9 5⎞ ⎛4 2 8 8⎞ ⎛5 1 6 4⎞ ⎛4 4 1 8⎞ ⎜2 3 7 3⎟ ⎜7 9 6 1⎟ ⎜2 4 3 8⎟ ⎜9 2 7 3⎟ ⎜9 4 3 4⎟ ⎜8 7 4 7⎟ ⎜9 3 6 6⎟ ⎜1 7 2 2⎟ ⎜9 1 8 7⎟ ⎜8 5 4 2⎟ ⎝2 3 8 3⎠ ⎝1 8 5 3⎠ ⎝3 2 8 3⎠ ⎝1 6 2 6⎠ ⎝5 5 3 3⎠
ReplaceAll(ans, 1, 0, 2)
⎛7 8 4 7⎞ ⎛7 6 9 5⎞ ⎛4 2 8 8⎞ ⎛5 0 6 4⎞ ⎛4 4 0 8⎞ ⎜2 3 7 3⎟ ⎜7 9 6 0⎟ ⎜2 4 3 8⎟ ⎜9 2 7 3⎟ ⎜9 4 3 4⎟ ⎜8 7 4 7⎟ ⎜9 3 6 6⎟ ⎜0 7 2 2⎟ ⎜9 0 8 7⎟ ⎜8 5 4 2⎟ ⎝2 3 8 3⎠ ⎝0 8 5 3⎠ ⎝3 2 8 3⎠ ⎝0 6 2 6⎠ ⎝5 5 3 3⎠