Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.2K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Automatic assignment of variables?

Posted 10 years ago

I would like to assign a value to a automatically created symbol. Let's say to bij where i and j are integers. I know I could do this with this code:

Do[b[i, j] = f[i, j], {i, n}, {j, n}];

But I prefer bij instead of b[i,j] because the code is shorter and becomes more readable. The problem is that with something like

 i = 1; j = 1;
   StringJoin["b", ToString[i], ToString[j]] = 2

or

 Symbol[StringJoin["b", ToString[i], ToString[j]]] = 2

or

ToExpression[StringJoin["b", ToString[i], ToString[j]]] = 2

one gets always the same error and the value 2 is not assigned to b11 in this case. So how can this be achieved?

POSTED BY: Ulrich Utiger
2 Replies
Posted 10 years ago

This works, thanks.

POSTED BY: Ulrich Utiger
In[3]:= Evaluate[
   ToExpression[StringJoin["b", ToString[i], ToString[j]]]] = 2;

In[4]:= bij

Out[4]= 2
POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard