Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum > General > > "Using a loop to fill an array"

Post Reply:
Name:
Email Address:

Please send email when my message is replied to.

Url (optional):
Subject:
Message: view original message?
Attachment (optional):
Please answer this:6+2 =



Original Message (ID '87606') By jf:
abc needs to already have as many elements as you want to assign to. In[1]:= abc = {0, 0, 0, 0, 0}; Do[abc[[i]] = i^2, {i, 1, 5}]; abc Out[3]= {1, 4, 9, 16, 25} The lazy way. In[4]:= abc = Range[5]^2 Out[4]= {1, 4, 9, 16, 25} Starting with an empty list and appending elements. General. Usually the slow way. In[5]:= def = {}; Do[ AppendTo[def, i^2], {i, 1, 5}]; def Out[7]= {1, 4, 9, 16, 25}