array
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(m_{1} .. n_{1}
, <m_{2} .. n_{2}, …
>) array(m_{1} .. n_{1}
, <m_{2} .. n_{2}, …
>,index_{1} = entry_{1}, index_{2} = entry_{2}, …
) array(m_{1} .. n_{1}
, <m_{2} .. n_{2}, …
>,List
) array(<m_{1} .. n_{1}, m_{2} .. n_{2}, …
>,ListOfLists
)
array(...)
creates an array, which is an ndimensional
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 m_{1} to n_{1},
the second index runs from m_{2} to n_{2},
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 m_{1} ≤ n_{1}, m_{2} ≤ n_{2}, 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 (n_{1}  m_{1} + 1) (n_{2}  m_{2} + 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 i_{1}
for
onedimensional arrays and (i_{1}, i_{2},
…)
for higherdimensional arrays, where i_{1},
i_{2}, …
are integers within valid boundaries,
satisfying m_{1} ≤ i_{1} ≤ n_{1}, m_{2} ≤ i_{2} ≤ n_{2},
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 (n_{1}  m_{1} +
1) (n_{2}  m_{2} +
1) … elements, each becoming an operand of the
array to be created. In case of twodimensional 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 kth
nesting level must be equal to the size of the kth
index range, that is, n_{k}  m_{k} +
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 onedimensional array is printed as a row vector. The index corresponds to the column number.
A twodimensional 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 onedimensional vectors or twodimensional matrices in the
mathematical sense.
Note the following special feature of arrays of domain type DOM_ARRAY
:
Create an uninitialized onedimensional array with indices ranging from 2 to 4:
A := array(2..4)
The NIL
s
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"])
Array boundaries can be specified by negative integers:
A := array(1..1, [2, sin(x), FAIL])
A[1], A[0], A[1]
delete A:
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]]); bool(%)
Note that all subfields of one dimension must have the same size and dimension. Therefore, the following input leads to an error:
array([[1],[3,4],[5,6]])
Error: Invalid argument. [array]
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])
Create a 2×2 matrix as a twodimensional 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: A
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)
Define a threedimensional 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
You can use a nested list to initialize a twodimensional array. The inner lists are the rows of the created matrix:
array(1..2, 1..3, [[1, 2, 3], [4, 5, 6]])
Create a threedimensional 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] ] ])
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)
A twodimensional 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
:
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 

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 
Object of type DOM_ARRAY
.