Create an array

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

For arrays in MATLAB®, see Matrices and Arrays (MATLAB).


array(m1 .. n1, <m2 .. n2, …>)
array(m1 .. n1, <m2 .. n2, …>, index1 = entry1, index2 = entry2, …)
array(m1 .. n1, <m2 .. n2, …>, List)
array(<m1 .. n1, m2 .. n2, …>, ListOfLists)


array(...) creates an array, which is an n-dimensional rectangular structure holding arbitrary data.

array(m_1..n_1, m_2..n_2, ...) creates an array with uninitialized entries, where the first index runs from m1 to n1, the second index runs from m2 to n2, and so on.

array( m_1..n_1, m_2..n_2 , ..., List) creates an array with entries initialized from List.

array(ListOfLists) creates an array with entries initialized from ListOfLists. The dimension of the array is the same as the dimension of ListOfLists.

Arrays are container objects for storing data. In contrast to tables, the indices must be sequences of integers. While tables can grow in size dynamically, the number of entries in an array created by array is fixed.

Arrays created by array are of domain type DOM_ARRAY. They may contain arbitrary MuPAD® objects as entries.

For an array A of type DOM_ARRAY or DOM_HFARRAY and a sequence of integers index forming a valid array index, an indexed call A[index] returns the corresponding entry. If the entry of an array of type DOM_ARRAY is uninitialized, then the indexed expression A[index] is returned. See Example 1 and Example 5.

An indexed assignment of the form A[index] := entry initializes or overwrites the entry corresponding to index. See Example 1 and Example 5.

The index boundaries must satisfy m1n1, m2n2, and so on. The dimension of the resulting array is the number of given range arguments; at least one range argument must be specified. The total number of entries of the resulting array is (n1 - m1 + 1) (n2 - m2 + 1) ….

If only index range arguments are given, then array creates an array with uninitialized entries. Entries are automatically set to 0.0 if no values are specified. See Example 1.

If equations of the form index = entry are present, then the array entry corresponding to index is initialized with entry. This is useful for selectively initializing some particular array entries.

Each index must be a valid array index of the form i1 for one-dimensional arrays and (i1, i2, …) for higher-dimensional arrays, where i1, i2, … are integers within valid boundaries, satisfying m1i1n1, m2i2n2, and so on, and the number of integers in index matches the dimension of the array.

If you use the argument List, then the resulting array is initialized with the entries from List. This is useful for initializing all array entries at once. List must have (n1 - m1 + 1) (n2 - m2 + 1) … elements, each becoming an operand of the array to be created. In case of two-dimensional arrays, regarded as matrices, the list contains the entries row after row.

The argument ListOfLists must be a nested list matching the structure of the array exactly. The nesting depth of the list must be greater or equal to the dimension of the array. The number of list entries at the k-th nesting level must be equal to the size of the k-th index range, that is, nk - mk + 1. See Example 7.

delete A[index] deletes the entry corresponding to index, so that it becomes uninitialized. See Example 5.


Internally, uninitialized entries of an array of domain type DOM_ARRAY have the value NIL. Thus assigning NIL to an array entry has the same effect as deleting it via delete. Afterwards, an indexed call of the form A[index] returns the symbolic expression A[index], and not NIL. See Example 5.

A one-dimensional array is printed as a row vector. The index corresponds to the column number.

A two-dimensional array is printed as a matrix. The first index corresponds to the row number, and the second index corresponds to the column number.

Big arrays that exceed the maximal output width TEXTWIDTH are printed in the form array( m_1..n_1, m_2..n_2, dots, index_1 = entry_1, index_2 = entry_2, dots ). See Example 6, Example 7, and Example 10.

Arithmetic operations are not defined for arrays of domain type DOM_ARRAY. Use matrix to create one-dimensional vectors or two-dimensional matrices in the mathematical sense.

Note the following special feature of arrays of domain type DOM_ARRAY:


If an array is evaluated, it is only returned. The evaluation does not map recursively on the array entries! This is due to performance reasons. You have to map the function eval explicitly on the array in order to fully evaluate its entries. See Example 8


Example 1

Create an uninitialized one-dimensional array with indices ranging from 2 to 4:

A := array(2..4)

The NILs in the output indicate that the array entries are not initialized. Set the middle entry to 5 and last entry to "MuPAD":

A[3] := 5: A[4] := "MuPAD": A

You can access array entries by using indexed calls. Because the entry A[2] is not initialized, the symbolic expression A[2] is returned:

A[2], A[3], A[4]

You can initialize an array already when creating it by passing initialization equations to array:

A := array(2..4, 3 = 5, 4 = "MuPAD")

You can initialize all entries of an array when creating it by passing a list of initial values to array:

array(2..4, [PI, 5, "MuPAD"])

Example 2

Array boundaries can be specified by negative integers:

A := array(-1..1, [2, sin(x), FAIL])

A[-1], A[0], A[1]

delete A:

Example 3

If the dimension and size of the array are not specified explicitly, then both values are taken from the given list:

array([[1,2],[3,4],[5,6]]) = array(1..3, 1..2, [[1,2],[3,4],[5,6]]);

Note that all subfields of one dimension must have the same size and dimension. Therefore, the following input leads to an error:

Error: Invalid argument. [array]

Example 4

You can use the $ operator to create a sequence of initialization equations:

array(1..8, i = i^2 $ i = 1..8)

Equivalently, you can use the $ operator to create an initialization list:

array(1..8, [i^2 $ i = 1..8])

Example 5

Create a 2×2 matrix as a two-dimensional array:

A := array(1..2, 1..2, (1, 2) = 42, (2, 1) = 1 + I)

Internally, array entries are stored in a linearized form. They can be accessed in this form via op. Uninitialized entries internally have the value NIL:

op(A, 1), op(A, 2), op(A, 3), op(A, 4)

Note the difference to the indexed access:

A[1, 1], A[1, 2], A[2, 1], A[2, 2]

Modify an array entry by an indexed assignment:

A[1, 1] := 0: A[1, 2] := 5:

Delete the value of an array entry via delete. Afterwards, it is uninitialized again:

delete A[2, 1]: A[2, 1], op(A, 3)

Assigning NIL to an array entry has the same effect as deleting it:

A[1, 2] := NIL: A[1, 2], op(A, 2)

Example 6

Define a three-dimensional array with index values between 1 and 8 in each of the three dimensions. Initialize two of the entries via initialization equations:

A := array(1..8, 1..8, 1..8, (1, 1, 1) = 111, (8, 8, 8) = 888)

A[1, 1, 1], A[1, 1, 2]

delete A

Example 7

You can use a nested list to initialize a two-dimensional array. The inner lists are the rows of the created matrix:

array(1..2, 1..3, [[1, 2, 3], [4, 5, 6]])

Create a three-dimensional array and initialize it from a nested list of depth three. The outer list has two entries for the first dimension. Each of these entries is a list with three entries for the second dimension. Finally, the innermost lists each have one entry for the third dimension:

array(2..3, 1..3, 1..1,
        [ [1], [2], [3] ],
        [ [4], [5], [6] ]

Example 8

If an array is evaluated, it is only returned. The evaluation does not map recursively on the array entries. Here, the entries a and b are not evaluated:

A := array(1..2, [a, b]):
a := 1:  b := 2:
A, eval(A)

Due to the special evaluation of arrays the index operator evaluates array entries after extracting them from the array:

A[1], A[2]

To fully evaluate its entries, map the function eval explicitly on the array:

map(A, eval)

Example 10

A two-dimensional array is usually printed in a matrix form:

A := array(1..4, 1..4, (1, 1) = 11, (4, 4) = 44)

If the output does not fit into TEXTWIDTH, a more compact output is used in print:

print(Plain, A)
array(1..4, 1..4, (\
1, 1) = 11, (4, 4) \
= 44)


m1, n1, m2, n2, …

The index boundaries: integers

index1, index2, …

A sequence of integers defining a valid array index

entry1, entry2, …

Arbitrary objects


A plain list of entries for initializing the array


A nested list (of lists of lists of …) of entries for initializing the array

Return Values

Object of type DOM_ARRAY.

See Also

MuPAD Domains

MuPAD Functions