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

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
jf
10/27/12 1:02pm

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}



URL: ,

Subject (listing for 'Using a loop to fill an array')
Author Date Posted
Using a loop to fill an array Miguel 10/26/12 6:49pm
Re: Using a loop to fill an array jf 10/27/12 1:02pm
Re: Re: Using a loop to fill an array Miguel 10/28/12 11:08pm
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic