1
$\begingroup$

I'm inserting a number into the first column of a table, but when I call the table again, the number is not there anymore. Can someone explain to me what I'm doing wrong here?

In[22]:= mass[n]

Out[22]= {5.07637*10^24, 8.24164*10^24, 2.4486*10^24, 3.70276*10^24, 
4.0611*10^24, 8.00275*10^24, 2.74721*10^24}

In[23]:= Insert[mass[n], 1.59084*10^29, 1]

Out[23]= {1.59084*10^29, 5.07637*10^24, 8.24164*10^24, 2.4486*10^24, 
3.70276*10^24, 4.0611*10^24, 8.00275*10^24, 2.74721*10^24}

In[24]:= mass[n][[1]]

Out[24]= 5.07637*10^24

In[25]:= mass[n]

Out[25]= {5.07637*10^24, 8.24164*10^24, 2.4486*10^24, 3.70276*10^24, 
4.0611*10^24, 8.00275*10^24, 2.74721*10^24}
$\endgroup$
  • 1
    $\begingroup$ Insert is not an in-place modification (these end usually with a To like AddTo). In your case, you may use mass[n] = Insert[mass[n], 1.59084*10^29, 1] or PrependTo[mass[n],1.59084*10^29] which are both not really optimal since they involve a copy of the whole list. So use that rarely for longer lists. If you can, try to build vectors, matrices, and more general arrays in one go (e.g. with Table) or at least boil it down to few Join or ArrayFlatten operations. $\endgroup$ – Henrik Schumacher Apr 6 '18 at 13:28

Browse other questions tagged or ask your own question.