Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.7K Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Storing data in variable name depending on loop index?

Posted 3 years ago
POSTED BY: Kasturi Banerjee
3 Replies
Posted 3 years ago

You could consider using indexed variables. Instead of naming the variables a1, a2 .. ai, name them a[1], a[2] .. a[i]. The index number in square brackets can then easliy be assigned from code. And the value of a[n] can likewise easily be retrieved.

enter image description here

POSTED BY: Hans Milton

Something like this?

In[28]:= vars = Table[Symbol["dddd" <> ToString[i]], {i, 1, 10}]

Out[28]= {dddd1, dddd2, dddd3, dddd4, dddd5, dddd6, dddd7, dddd8, \
dddd9, dddd10}

In[29]:= values = Table[i*3, {i, 1, 10}]

Out[29]= {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}

In[31]:= MapThread[Set[#1, #2] &, {vars, values}]

Out[31]= {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}

In[33]:= vars

Out[33]= {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}

or in one line, you need to first evaluate the Symbols:

Table[Evaluate[Symbol["dddde" <> ToString[i]]] = i*3, {i, 1, 10}]

Regards

POSTED BY: Neil Singer

Welcome to Wolfram Community!

Please include your code and output using one of the methods explained in the rules: http://wolfr.am/READ-1ST

POSTED BY: EDITORIAL BOARD
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard