This section shows us how to create matrices and arrays and
how to do simple matric calculations.
You find all the following examples in
XLGmatrix01.xpl
and
XLGmatrix02.xpl
.
|
We create a matrix or an array of
dimensions
, i.e. up to
eight dimensions with the function
matrix
. All the elements of
the created array are set to 1.
The instruction
mat=matrix(2,3) matgenerates a
Contents of mat [1,] 1 1 1 [2,] 1 1 1The definition of higher-dimensional matrices is straightforward. However, as we cannot represent three-dimensional (or higher-dimensional) objects in the two-dimensional space of this page, or on your screen, the first two dimensions are displayed in the standard format, the projections of the higher ones are then displayed. Consider the following matrix of dimension
array = matrix(2,3,2) arrayThis matrix is displayed in the form of two submatrices, the layers of the matrix: The first layer contains the elements of array
Contents of array [,,1,1,1,1,1,1] [1,] 1 1 1 [2,] 1 1 1 [,,2,1,1,1,1,1] [1,] 1 1 1 [2,] 1 1 1
The functions
uniform
and
normal
have the same syntax
as matrix. They are used to generate pseudorandom uniform or normal
(Gaussian) numbers, respectively. The seed for the the random number
generator can be set by
randomize
. For example,
randomize(111222) normal(2,2,2)produces
Contents of rnorm [,,1,1,1,1,1,1] [1,] 0.23144 1.1671 [2,] -0.38991 -0.0089669 [,,2,1,1,1,1,1] [1,] 0.60053 -0.54146 [2,] -0.28422 1.0531
A matrix , with typical element
, is said to be diagonal
if all elements outside its main diagonal are equal to zero,
i.e.
. We create a diagonal matrix
with the function
diag
, whose argument is the ordered sequence
of elements in the main diagonal:
x = #(1, 2, 3) diag (x)which yields
[1,] 1 0 0 [2,] 0 2 0 [3,] 0 0 3
A particular diagonal matrix is the identity matrix denoted by
: The elements of its main diagonal are all equal to one. We create
a unit matrix with the function
unit
, which has as argument the
dimension of the matrix. Since this function belongs to the
xplore
library, we need to load that library before using it. The commands
library("xplore") id=unit(3)create the three-dimensional identity matrix:
Contents of id [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1
We display the dimension of the matrix, i.e. the number of
elements in each direction, with the function
dim
. This function
has the matrix as its argument and returns its dimensions. Thus, the
instruction
dim(mat)returns
Contents of dim [1,] 2 [2,] 3which means that the first dimension of mat is equal to 2, and the second dimension is equal to 3.
The function
dim
nests the functions
rows
and
cols
, which respectively return the number of rows and columns
of a matrix. Thus,
rows(mat)yields
Contents of rows [1,] 2You can check that cols(mat) returns 3.
The operators |and
can be used to
concatenate matrices or arrays in vertical or horizontal
direction. We have used this already for creating simple
data matrices in
Descriptive Statistics (2).
Both operators also work for matrices:
unit(3)~matrix(3)returns
Contents of _tmp [1,] 1 0 0 1 [2,] 0 1 0 1 [3,] 0 0 1 1whereas
diag(1:3)|matrix(1,3)gives
Contents of _tmp [1,] 1 0 0 [2,] 0 2 0 [3,] 0 0 3 [4,] 1 1 1
The functions
dim
,
rows
,
cols
as well as the
operators |and
can also be applied to
alphanumeric matrices, i.e. multidimensional objects containing text.
For example,
textmat = #("aa","c") ~ #("b","d2") dim(textmat)creates an alphanumeric matrix and prints its dimension
Contents of dim [1,] 2 [2,] 2However, matrices cannot mix alphanumeric and numeric elements. We will see in Section 16.7 that numeric and alphanumeric elements can only be mixed in lists.
|
Let and
be two matrices of the same dimension
, with
typical elements
and
. Given that these two matrices
are of the same dimension, they are said to be conformable for addition
and subtraction. The matrix
, of dimension
, defined
as
, obtained by the addition of matrices
and
,
is the matrix with typical element
such that
. The matrix
, of dimension
, defined as
, is the matrix with typical element
such that
. For example
A = (2|4)~(8|6) B = (5|6)~(7|8) A + Breturns
Contents of _tmp [1,] 7 15 [2,] 10 14You can use the elementwise operators also for adding and subtracting scalars and conformable vectors. For example
A + 3 A - (1|1)gives
Contents of _tmp [1,] 5 11 [2,] 7 9 Contents of _tmp [1,] 1 7 [2,] 3 5
Two matrices and
are said to be conformable for the matrix
multiplication
, if the number of columns of
is equal to the
number of rows
.
The matrix
defined as
has as typical element
, with
.
Let's define the following matrices:
E = #(2, 4)~#(8, 6) F = #(1, 2)~#(3, 4)Since both matrices are conformable for the two multiplications
mult1 = F * F mult2 = E * E mult1 mult2shows
Contents of mult1 [1,] 18 38 [2,] 16 36 Contents of mult2 [1,] 14 26 [2,] 20 40The two matrices mult1 and mult2 are indeed different.
Matrix multiplication is different from matrix elementwise
multiplication, the operator of which is the compound operator
.*
obtained by concatenating the dot and star symbols. Two matrices
are said conformable for elementwise multiplication if they are
of the same dimension. The matrix , defined by
, has
typical element
, where
. Matrix
elementwise multiplication is obviously commutative:
mult3 = E.*F mult4 = F.*E mult3 mult4shows
Contents of mult3 [1,] 2 24 [2,] 8 24 Contents of mult4 [1,] 2 24 [2,] 8 24The matrices mult3 and mult4 are the same, and differ from both mult1 and mult2.
Note that all elementwise operations extend to arrays in a natural way. Typical matrix operations, such as matrix multiplication, are performed for each layer of an array. This holds also for the determinant, the inverse and the transpose which we will consider now.
A square matrix is such that its number of rows
is equal to its number of columns.
A square matrix is said to be invertible if its determinant,
denoted by
or
, is different from zero. The
determinant of a matrix measures the ``volume'' of the space spanned by
its column vectors. The determinant of a matrix is thus equal to zero
if its rows, or column vectors, are collinear.
We calculate the determinant of a matrix with the function
det
,
which has the matrix as its argument. Thus,
det(A)returns
Contents of det [1,] -20
The inverse of an invertible matrix is the unique matrix,
denoted by
of the same dimension of
, such that
=
, the identity matrix. We evaluate the inverse of a
matrix with the function
inv
. The matrix
is invertible since
its determinant is different from zero. We calculate this inverse as
follows:
inv(A)yields
Contents of cinv [1,] -0.3 0.4 [2,] 0.2 -0.1We verify that the product of
A*inv(A)gives
Contents of _tmp [1,] 1 0 [2,] -1.1102e-16 1which is numerically close to the two-dimensional identity matrix. The difference in the element in the second row and the first column is caused by rounding errors.
The transpose of a matrix , with typical element
, is
the matrix denoted by
: the element in the
th row and
th column of
is
, the element in the
th row and
th column of
. The matrix
is said to be symmetric if and
only if
.
We calculate the transpose of a matrix with either the function
trans
or the operator '.
The following two instructions are equivalent:
trans(A) A'and return the transpose of the matrix A.