This is really more of an addendum to @eyorble's perfectly good answer. I wanted to add a bit of an example to illustrate what's going on.
Suppose you have a matrix of variables and a corresponding matrix of values that they take:
vars = Partition[ToExpression[CharacterRange["α", "ι"]], 3]
nums = Partition[Range[9], 3]
(* {{α, β, γ},
{δ, ε, ζ},
{η, θ, ι}}
{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}} *)
(OP called my vars
matrix M
in the question, but I've changed it to vars
because single capital letters should be avoided in Mathematica, and it's nice to have meaningful variable names.)
Now suppose that your resultsymbolic
is a matrix of some horrific functions of these variables:
resultssymbolic = Partition[f @@@ Table[RandomSample[Flatten[vars], 4], {4}], 2]
(* {{f[ι, ζ, ε, θ], f[ζ, γ, θ, α]},
{f[δ, θ, ζ, α], f[β, δ, η, α]}} *)
Then the numerical values can be substituted in with
resultssymbolic /. Flatten@MapThread[Rule, {vars, nums}, 2]
(* {{f[9, 6, 5, 8], f[6, 3, 8, 1]},
{f[4, 8, 6, 1], f[2, 4, 7, 1]}} *)
The key to making the substitution work is the list of replacement rules, which I constructed by MapThread
ing the Rule
function over vars
and nums
(at level 2):
Flatten@MapThread[Rule, {vars, nums}, 2]
(* {α -> 1, β -> 2, γ -> 3, δ -> 4, ε -> 5, ζ -> 6, η -> 7, θ -> 8, ι -> 9} *)
Notice that without the Flatten
we'd end up with a list of replacement lists:
(* {{α -> 1, β -> 2, γ -> 3},
{δ -> 4, ε -> 5, ζ -> 6},
{η -> 7, θ -> 8, ι -> 9}} *)
Trying to use this to replace the values will fail because ReplaceAll
will interpret it as three separate replacement rules, and return three distinct matrices:
resultssymbolic /. MapThread[Rule, {vars, nums}, 2]
(* {{{f[ι, ζ, ε, θ], f[ζ, 3, θ, 1]},
{f[δ, θ, ζ, 1], f[2, δ, η, 1]}},
{{f[ι, 6, 5, θ], f[6, γ, θ, α]},
{f[4, θ, 6, α], f[β, 4, η, α]}},
{{f[9, ζ, ε, 8], f[ζ, γ, 8, α]},
{f[δ, 8, ζ, α], f[β, δ, 7, α]}}
} *)
which is clearly not what we're after.
nums
relate to the symbols inresultsymbolic
. Is each entry innums
a value for a variable? Can you give a minimal example (ie,B == 2
or3
)? Also, I think you mean/.
, and not\.
. $\endgroup$