Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Given an expression in x, y, z (for example), and a list with three elements, like {1,2,3}, how can I do the equivalent of expr/.{x->1,y->2,z->3}? So for example suppose

a = {1,2,3}
x^2+y^2+z^2 /.{x->1,y->2,z->3}

but obviously x^2+y^2+z^2/.{x,y,z}->a does not work.

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You can do this:

a = {1, 2, 3}
x^2 + y^2 + z^2 /. Rule @@@ Transpose[{{x, y, z}, a}]

You might also use MapThread and the like. But anyways, make sure you understand what @@@ does.

share|improve this answer
    
Thanks. Beyond your suggestion to understand @@@, I also just learned that -> is really infix shorthand for Rule[]. –  rogerl May 4 '13 at 1:43
add comment

This can be found in many answers, but I can't find the same question on this site or in the documentation. So:

x^2+y^2+z^2 /.Thread[{x, y, z} -> a]
  (* 14 *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.