在数学中,
- 如何创建长度为 n 的列表并用零填充?
- 如何创建长度为 n 的向量并用零填充?
版本 6.0 及更高版本包含一个新功能ConstantArray
,用于执行此操作,并且比使用更有效Table
:
In[2]:= ConstantArray[0,10]
Out[2]= {0,0,0,0,0,0,0,0,0,0}
此处的文档:
http ://reference.wolfram.com/mathematica/ref/ConstantArray.html
在 Mathematica 中,列表和向量之间没有区别。您可以使用该Table
函数生成长度为 n 的列表:
x = Table[0, {n}]
(* If n was 4, x would now be the list {0, 0, 0, 0} *)