2
$\begingroup$

I used a for loop to make 4 variables:

   For[i = 0, i < 4, i++,bi=0]

But when I write b0 or b1 it print its as b0 or b1 not 0 or 0.

$\endgroup$
3
  • $\begingroup$ possible duplicate: 783 $\endgroup$
    – Kuba
    Oct 14, 2022 at 9:57
  • $\begingroup$ An alternative approach is to use Array[a, 10]. Now you have created 10 different a's that you can use as a[3] or a[7]. $\endgroup$
    – bill s
    Oct 14, 2022 at 16:30
  • $\begingroup$ Mathematica is different from explicit programming (C, Python, etc.): in Mathematica there is no need for such explicit assignments. In this case, for example you could define b[i_] = 0 to make every value zero (as a pattern), or you could restrict it to your values with b[i_Integer /; 0 <= i < 4] = 0. Please read an introduction to the Wolfram language. $\endgroup$
    – Roman
    Oct 15, 2022 at 6:44

2 Answers 2

6
$\begingroup$

You might find it easier to do

For[i = 0, i < 4, i++,b[i]=0]

b[3] (* gives 0 *)
$\endgroup$
3
$\begingroup$

You could use ToExpression

For[i = 0, i < 4, i++, ToExpression["b" <> ToString[i] <> "=" <> ToString[i]]]

and now

Mathematica graphics

I am not sure why you want to do the above, there could be better ways to solve the problem you are trying to solve if you explain the purpose of doing this.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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