ConstantArray

ConstantArray[c,n]

generates a list of n copies of the element c.

ConstantArray[c,{n1,n2,}]

generates an array of nested lists containing copies of the element c.

Details

Examples

open allclose all

Basic Examples  (3)

Make a vector of 10 c's:

Make a 3×4 array of c's:

Make an array out of blocks:

Scope  (4)

A machine-zero vector:

A vector of exact zeros:

A machine-zero matrix:

A matrix of exact zeros:

A vector of ones:

A machine-number vector of ones:

A deeply nested constant array:

Applications  (3)

Set up an array to use as storage in a program:

Numerically solve x'[t]==1/(t+x[t]^2) using Euler's method:

Find the sum of the elements in a list:

This can also be done with Total:

Use a vector of ones to see the effects of conditioning on the solution of a linear system:

The solution at machine precision has significant errors:

At higher precision, the solution is much better:

Properties & Relations  (3)

ConstantArray[c,dims] has Dimensions[dims]:

ConstantArray[c,dims] is equal to SparseArray[{},dims,c]:

Normal[s] is identical to ConstantArray[c,dims]:

ConstantArray[c,dims] is equivalent to Apply[Table[c,##]&,Map[List,dims]]:

Introduced in 2007
 (6.0)
 |
Updated in 2008
 (7.0)