Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mathematica -creating lists and vectors of specific length

In Mathematica,

  1. How can I create a list of length n and fill with zeroes?
  2. How can I create a vector of length n and fill with zeroes?
like image 673
Mary A. Marion Avatar asked Feb 27 '10 20:02

Mary A. Marion


2 Answers

Version 6.0 and up include a new function ConstantArray for doing exactly this, and is more efficient than using Table:

In[2]:= ConstantArray[0,10]
Out[2]= {0,0,0,0,0,0,0,0,0,0}

Documentation here:
http://reference.wolfram.com/mathematica/ref/ConstantArray.html

like image 170
Michael Pilat Avatar answered Sep 30 '22 15:09

Michael Pilat


In Mathematica, there's no distinction between lists and vectors. You can use the Table function to generate a list of length n:

x = Table[0, {n}]
(* If n was 4, x would now be the list {0, 0, 0, 0} *)
like image 23
Adam Rosenfield Avatar answered Sep 30 '22 14:09

Adam Rosenfield