Many computer applications require large amounts of numerical data laid
out in two-dimensional arrays. Computer graphics, mathematical graph
theory, and probability/statistics are just some of these applications.
A
matrix is a two-dimensional structure of numerical
values. Below is a
4 × 3 matrix (4 rows, 3 columns)
with row and column
numbers shown in black, and matrix elements shown in red. Note:
- The row and column numbers begin with zero
- If this matrix is named A, its elements are referred to by
aij, where i and j are
the element's row and column numbers, respectively.
Thus the upper left element of A is referred to
as a00, and the lower right element as
a32.
The
transpose of an
m × n matrix
A is
an
n × m matrix whose
ith column has the same
values as
A's
ith row.
More formally, if
B is the transpose of
A, then for all
elements
bij of
B,
bij = aji.
Also, if
B is the transpose of
A, then
A is the
transpose of
B.
For example, the matrices below are transposes of each other.
An
m × m (i.e.
square) matrix is an
identity
matrix if all of its elements are zeros (0), except for those along the
diagonal, which all are ones (1).
More formally, if
A is an identity matrix, then all
elements
aii equal 1, while the rest equal 0.
Shown below is a
4 × 4 identity matrix. An identity matrix
is so-called because when it is multiplied by a suitable
m ×
k matrix
M (see matrix multiplication below), the result
is just
M.
Two matrices are equal if they have the same dimensions and their
elements are pair-wise equal.
That is, matrix
A is equal to
B if
A and
B
have the same dimensions and
aij = bij for
all
i and
j within range.
Examples are shown in the menu to the left. Note that the examples are
snapshots of a matrix manipulation tool you will use in a lab exercise and
programming assignment.
In this example, the matrices are not equal because the elements in the
bottom right location are not equal.
In this example, all elements are equal so the matrices are equal.
In this example, the matrices' dimensions are not the same, so the
matrices are not equal.
To add two matrices, they must have the same dimensions, i.e. the same
number of rows and columns.
The result of adding matrices
A and
B is a
matrix
C of the same dimensions, where
cij =
aij + bij.
For example:
To multiply two matrices, the number of columns in the first matrix
must equal the number of rows in the second matrix. Matrix
multiplication is not commutative.
For example, consider these matrices:
The product of
Matrix 1 and
Matrix 2 is
defined, but the product of
Matrix 2 and
Matrix
1 is not.
The result of multiplying an
m×n
matrix
A and an
n×p
matrix
B is an
m×p matrix
C
where
cij is the dot product of
the
ith row of
A and
the
jth column of
B.
For example,
- The third row (index 2) of Matrix 1 contains
the values 6 5 1
- The first column (index 0) of Matrix 2 contains
the values 2 4 3
- The dot product of
6 5 1
and
2 4 3
=
6×2 + 5×4
+ 1×3
=
12 + 20 + 3
=
35
- So the first column of the third row
of Result = 35
Computing all possible dot products yields:
If
Matrix 1 is an
m×n matrix and
Matrix 2 is an
n×n identity matrix,
then the result of multiplying
Matrix 1 by
Matrix
2 is
Matrix 1.
See the menu at left for examples.
This section describes simple actions for automatically setting the
elements of matrices.
We can fill a matrix "row-wise" by:
- Setting the upper left element to 1 and
incrementing successive elements of the first row by 1 from left to right
- When a row is filled, move to the next row and repeat, starting
with the last element value plus 1
Here is the result of filling a 4×3 matrix row-wise:
We can fill a matrix "column-wise" by:
- Setting the upper left element to 1 and
incrementing successive elements of the first column by 1 from top to bottom
- When a column is filled, move to the next column and repeat, starting
with the last element value plus 1
Here is the result of filling a 4×3 matrix column-wise: