Mathematica Asked on August 12, 2021
One can create new variable names by Symbol :
In[1] Table[Symbol["x"<>ToString[i]], {i,1,5}]
Out[1] {x1,x2,x3,x4,x5}
But it is also possible to use ToExpression (old firend) :
In[2] Table[ToExpression["x"<>ToString[i]], {i,1,5}]
Out[2] {x1,x2,x3,x4,x5}
There is a difference : Symbol will produce an error for incorrect varialbe names while ToExpression will do something meaningful.
In[3] Table[Symbol[ToString[i]<>"x"], {i,1,5}]
Out[3] (* some error message with non-helpful output)
In[4] Table[ToExpression[ToString[i]<>"x"], {i,1,5}]
Out[4] {x,2x,3x,4x,5x}
Other than this, I see there is no difference. Is there a situation such that Symbol is more useful than ToExpression ?
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP