Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple assignment between lists in Mathematica 7

Suppose there are two lists a = {a1, a2, a3} and b = {b1, b2, b3}, and I want to write an assignment statement to make a1=b1,a2=b2,a3=b3 which only refers to a and b:

Thread[a = b]

But it only makes a={b1,b2,b3}. Using := (SetDelayed) instead of = doesn't work either.
Any solution? Thanks.

like image 515
Explogit Avatar asked Feb 28 '23 03:02

Explogit


2 Answers

I think the Thread only works on "explicit" lists; the variables need to be expanded before being operated on.

After some experimentation, this works for me:

a = {a1, a2, a3};
b = {b1, b2, b3};
Thread[Set[Evaluate@a, Evaluate@b]];
{a1, a2, a3}

You could also write Thread[Evaluate@a = Evaluate@b]; just depends whichever you find more readable.

like image 108
Will Robertson Avatar answered Mar 02 '23 15:03

Will Robertson


What's wrong with

MapThread[Set,{{a1,a2,a3},{b1,b2,b3}}]

?

like image 37
none Avatar answered Mar 02 '23 17:03

none