Create an array
For arrays in MATLAB®, see Matrices and Arrays.
MuPAD® notebooks are not recommended. 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.
m1 .. n1, <
m2 .. n2, …>) array(
m1 .. n1, <
m2 .. n2, …>,
index1 = entry1, index2 = entry2, …) array(
m1 .. n1, <
m2 .. n2, …>,
m1 .. n1, m2 .. n2, …>,
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
array(ListOfLists) creates an array with
entries initialized from
ListOfLists. The dimension
of the array is the same as the dimension of
Arrays are container objects for storing data. In contrast to
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
They may contain arbitrary MuPAD® objects as entries.
For an array
A of type
a sequence of integers
index forming a valid array
index, an indexed call
the corresponding entry. If the entry of an array of type
uninitialized, then the indexed expression
returned. See Example 1 and Example 5.
The index boundaries must satisfy m1 ≤ n1, m2 ≤ n2, 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
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
= entry are present, then the array entry corresponding
index is initialized with
This is useful for selectively initializing some particular array
Each index must be a valid array index of the form
one-dimensional arrays and
…) for higher-dimensional arrays, where
i2, … are integers within valid boundaries,
satisfying m1 ≤ i1 ≤ n1, m2 ≤ i2 ≤ n2,
and so on, and the number of integers in
the dimension of the array.
If you use the argument
List, then the resulting
array is initialized with the entries from
This is useful for initializing all array entries at once.
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.
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
index, so that it becomes uninitialized. See Example 5.
Internally, uninitialized entries of an array of domain type
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
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.
Note the following special feature of arrays of domain type
Create an uninitialized one-dimensional array with indices ranging from 2 to 4:
A := array(2..4)
in the output indicate that the array entries are not initialized.
Set the middle entry to 5 and
last entry to
A := 5: A := "MuPAD": A
You can access array entries by using indexed calls. Because
A is not initialized, the symbolic
A is returned:
A, A, A
You can initialize an array already when creating it by passing
initialization equations to
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(2..4, [PI, 5, "MuPAD"])
Array boundaries can be specified by negative integers:
A := array(-1..1, [2, sin(x), FAIL])
A[-1], A, A
If the dimension and size of the
not specified explicitly, then both values are taken from the given
array([[1,2],[3,4],[5,6]]) = array(1..3, 1..2, [[1,2],[3,4],[5,6]]); bool(%)
Note that all subfields of one dimension must have the same size and dimension. Therefore, the following input leads to an error:
Error: The argument is invalid. [array]
You can use the
$ operator to create a sequence of initialization
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])
Create a 2×2 matrix as a two-dimensional array:
A := array(1..2, 1..2, (1, 2) = 42, (2, 1) = 1 + I)
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: A
Delete the value of an array entry via
delete. Afterwards, it is uninitialized
delete A[2, 1]: A[2, 1], op(A, 3)
an array entry has the same effect as deleting it:
A[1, 2] := NIL: A[1, 2], op(A, 2)
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]
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, [ [ , ,  ], [ , ,  ] ])
If an array is evaluated, it is only returned. The evaluation
does not map recursively on the array entries. Here, the entries
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 two-dimensional array is usually printed in a matrix form:
A := array(1..4, 1..4, (1, 1) = 11, (4, 4) = 44)
PRETTYPRINT := FALSE: TEXTWIDTH := 20: print(Plain, A)
array(1..4, 1..4, (\ 1, 1) = 11, (4, 4) \ = 44)
PRETTYPRINT := TRUE:
delete A, TEXTWIDTH
The index boundaries: integers
A sequence of integers defining a valid array index
A plain list of entries for initializing the array
A nested list (of lists of lists of …) of entries for initializing the array
Object of type