2
$\begingroup$

I have a huge array of variables like this.

variables=Flatten[Join[
 Flatten[Table[
    alpha[i, j, k, l, m, n], {i, 0, 2}, {j, 0, 2}, {k, 0, 2}, {l, 0, 
     2}, {m, 0, 2}, {n, 0, 2}]], 
  Flatten[Table[
    beta[i, j, k, l, m, n], {i, 0, 1}, {j, 0, 1}, {k, 0, 1}, {l, 0, 
     1}, {m, 0, 1}, {n, 0, 1}]], {gamma}]]

I have an array of values, for the example let's take:

values=Flatten[Join[
 Flatten[Table[
    1, {i, 0, 2}, {j, 0, 2}, {k, 0, 2}, {l, 0, 
     2}, {m, 0, 2}, {n, 0, 2}]], 
  Flatten[Table[
    1, {i, 0, 1}, {j, 0, 1}, {k, 0, 1}, {l, 0, 
     1}, {m, 0, 1}, {n, 0, 1}]], {gamma}]]

How can I assign each one of my variables inside my array corresponding values from values?

If I do variables=values, it will not set values to the alpha's or beta's but it will just change the variable "variables".

How can I say to mathematica to change the values of the variables alpha's and beta's stored in "variables" to affect them the value "1".

I could do a loop but I think there is a better option?

$\endgroup$
2
$\begingroup$

variables itself is a variable. When you do variables=values, you simply assign a value to it. You have to assign values to each of its elements. Something like as you suggested

Do[variables[[n]] = values[[n]], {n,Length[variables]}]

A better way is using MapThread which map elements from each array.

MapThread[(#1 = #2) &, {variables, values}]

alpha[0, 0, 0, 0, 1, 0]

1

$\endgroup$
0
$\begingroup$

you can also do this:

Inner[Set, Most@variables, Most@values, None];
$\endgroup$
0
$\begingroup$
Clear[alpha, beta, gamma]
Evaluate[variables] = values;

If the variables are still symbols with no value assigned the first line can be left out.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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