I wonder define a bunch variables named like $X_1$, $X_2$, $...$, $X_n$. Is there some smart way to declare them instead of by pure typing. One trick I can image is to use the for loop,For [i = 1, i <= n, i++], to replace the subindices,. However, now each i is not numbers but texts, I don't know how to implement the for loop to texts or text strings. Can anyone give me some hints?
Asked
Active
Viewed 924 times
0
-
1Please avoid using subscripts. Some feferences: ref1, ref2, ref3 – Dr. belisarius Oct 20 '15 at 11:40
1 Answers
3
This would be more idiomatic to Mathematica than using a for loop:
indices = {"a", "b", "c"};
Array[(x[indices[[#]]] = #) &, 3];
{x["a"], x["b"], x["c"]}
{1, 2, 3}
This is an equivalent for loop:
For[i = 1, i <= Length[indices], i++,
x[indices[[i]]] = i]
Chris Degnen
- 30,927
- 2
- 54
- 108