Documentation |
On this page… |
---|
Adding and Subtracting Matrices Using Multithreaded Computation with Linear Algebra Functions |
The MATLAB^{®} environment uses the term matrix to indicate a variable containing real or complex numbers arranged in a two-dimensional grid. An array is, more generally, a vector, matrix, or higher dimensional grid of numbers. All arrays in MATLAB are rectangular, in the sense that the component vectors along any dimension are all the same length.
Symbolic Math Toolbox™ software extends the capabilities of MATLAB software to matrices of mathematical expressions.
MATLAB has dozens of functions that create different kinds of matrices. There are two functions you can use to create a pair of 3-by-3 example matrices for use throughout this chapter. The first example is symmetric:
A = pascal(3) A = 1 1 1 1 2 3 1 3 6
The second example is not symmetric:
B = magic(3) B = 8 1 6 3 5 7 4 9 2
Another example is a 3-by-2 rectangular matrix of random integers:
C = fix(10*rand(3,2)) C = 9 4 2 8 6 7
A column vector is an m-by-1 matrix, a row vector is a 1-by-n matrix, and a scalar is a 1-by-1 matrix. The statements
u = [3; 1; 4] v = [2 0 -1] s = 7
produce a column vector, a row vector, and a scalar:
u = 3 1 4 v = 2 0 -1 s = 7
For more information about creating and working with matrices, see Creating and Concatenating Matrices.
Addition and subtraction of matrices is defined just as it is for arrays, element by element. Adding A to B, and then subtracting A from the result recovers B:
A = pascal(3); B = magic(3); X = A + B X = 9 2 7 4 7 10 5 12 8 Y = X - A Y = 8 1 6 3 5 7 4 9 2
Addition and subtraction require both matrices to have the same dimension, or one of them be a scalar. If the dimensions are incompatible, an error results:
C = fix(10*rand(3,2)) X = A + C Error using plus Matrix dimensions must agree. w = v + s w = 9 7 6
A row vector and a column vector of the same length can be multiplied in either order. The result is either a scalar, the inner product, or a matrix, the outer product :
u = [3; 1; 4]; v = [2 0 -1]; x = v*u x = 2 X = u*v X = 6 0 -3 2 0 -1 8 0 -4
For real matrices, the transpose operation interchanges a_{ij} and a_{ji}. MATLAB uses the apostrophe operator (') to perform a complex conjugate transpose, and uses the dot-apostrophe operator (.') to transpose without conjugation. For matrices containing all real elements, the two operators return the same result.
The example matrix A is symmetric, so A' is equal to A. But, B is not symmetric:
B = magic(3); X = B' X = 8 3 4 1 5 9 6 7 2
Transposition turns a row vector into a column vector:
x = v' x = 2 0 -1
If x and y are both real column vectors, the product x*y is not defined, but the two products
x'*y
and
y'*x
are the same scalar. This quantity is used so frequently, it has three different names: inner product, scalar product, or dot product.
For a complex vector or matrix, z, the quantity z' not only transposes the vector or matrix, but also converts each complex element to its complex conjugate. That is, the sign of the imaginary part of each complex element changes. So if
z = [1+2i 7-3i 3+4i; 6-2i 9i 4+7i] z = 1.0000 + 2.0000i 7.0000 - 3.0000i 3.0000 + 4.0000i 6.0000 - 2.0000i 0 + 9.0000i 4.0000 + 7.0000i
then
z' ans = 1.0000 - 2.0000i 6.0000 + 2.0000i 7.0000 + 3.0000i 0 - 9.0000i 3.0000 - 4.0000i 4.0000 - 7.0000i
The unconjugated complex transpose, where the complex part of each element retains its sign, is denoted by z.':
z.' ans = 1.0000 + 2.0000i 6.0000 - 2.0000i 7.0000 - 3.0000i 0 + 9.0000i 3.0000 + 4.0000i 4.0000 + 7.0000i
For complex vectors, the two scalar products x'*y and y'*x are complex conjugates of each other, and the scalar product x'*x of a complex vector with itself is real.
Multiplication of matrices is defined in a way that reflects composition of the underlying linear transformations and allows compact representation of systems of simultaneous linear equations. The matrix product C = AB is defined when the column dimension of A is equal to the row dimension of B, or when one of them is a scalar. If A is m-by-p and B is p-by-n, their product C is m-by-n. The product can actually be defined using MATLAB for loops, colon notation, and vector dot products:
A = pascal(3); B = magic(3); m = 3; n = 3; for i = 1:m for j = 1:n C(i,j) = A(i,:)*B(:,j); end end
MATLAB uses a single asterisk to denote matrix multiplication. The next two examples illustrate the fact that matrix multiplication is not commutative; AB is usually not equal to BA:
X = A*B X = 15 15 15 26 38 26 41 70 39 Y = B*A Y = 15 28 47 15 34 60 15 28 43
A matrix can be multiplied on the right by a column vector and on the left by a row vector:
u = [3; 1; 4]; x = A*u x = 8 17 30 v = [2 0 -1]; y = v*B y = 12 -7 10
Rectangular matrix multiplications must satisfy the dimension compatibility conditions:
C = fix(10*rand(3,2)); X = A*C X = 17 19 31 41 51 70 Y = C*A Error using mtimes Inner matrix dimensions must agree.
Anything can be multiplied by a scalar:
s = 7; w = s*v w = 14 0 -7
Generally accepted mathematical notation uses the capital letter I to denote identity matrices, matrices of various sizes with ones on the main diagonal and zeros elsewhere. These matrices have the property that AI = A and IA = A whenever the dimensions are compatible. The original version of MATLAB could not use I for this purpose because it did not distinguish between uppercase and lowercase letters and i already served as a subscript and as the complex unit. So an English language pun was introduced. The function
eye(m,n)
returns an m-by-n rectangular identity matrix and eye(n) returns an n-by-n square identity matrix.
The Kronecker product, kron(X,Y), of two matrices is the larger matrix formed from all possible products of the elements of X with those of Y. If X is m-by-n and Y is p-by-q, then kron(X,Y) is mp-by-nq. The elements are arranged in the following order:
[X(1,1)*Y X(1,2)*Y . . . X(1,n)*Y . . . X(m,1)*Y X(m,2)*Y . . . X(m,n)*Y]
The Kronecker product is often used with matrices of zeros and ones to build up repeated copies of small matrices. For example, if X is the 2-by-2 matrix
X = 1 2 3 4
and I = eye(2,2) is the 2-by-2 identity matrix, then the two matrices
kron(X,I)
and
kron(I,X)
are
1 0 2 0 0 1 0 2 3 0 4 0 0 3 0 4
and
1 2 0 0 3 4 0 0 0 0 1 2 0 0 3 4
The p-norm of a vector x,
$${\Vert x\Vert}_{p}={\left({\displaystyle \sum {\left|{x}_{i}\right|}^{p}}\right)}^{1/p},$$
is computed by norm(x,p). This is defined by any value of p > 1, but the most common values of p are 1, 2, and ∞. The default value is p = 2, which corresponds to Euclidean length:
v = [2 0 -1]; [norm(v,1) norm(v) norm(v,inf)] ans = 3.0000 2.2361 2.0000
The p-norm of a matrix A,
$${\Vert A\Vert}_{p}=\underset{x}{\mathrm{max}}\frac{{\Vert Ax\Vert}_{p}}{{\Vert x\Vert}_{p}},$$
can be computed for p = 1, 2, and ∞ by norm(A,p). Again, the default value is p = 2:
C = fix(10*rand(3,2)); [norm(C,1) norm(C) norm(C,inf)] ans = 19.0000 14.8015 13.0000
MATLAB software supports multithreaded computation for a number of linear algebra and element-wise numerical functions. These functions automatically execute on multiple threads. For a function or expression to execute faster on multiple CPUs, a number of conditions must be true:
The function performs operations that easily partition into sections that execute concurrently. These sections must be able to execute with little communication between processes. They should require few sequential operations.
The data size is large enough so that any advantages of concurrent execution outweigh the time required to partition the data and manage separate execution threads. For example, most functions speed up only when the array contains several thousand elements or more.
The operation is not memory-bound; processing time is not dominated by memory access time. As a general rule, complicated functions speed up more than simple functions.
The matrix multiply (X*Y) and matrix power (X^p) operators show significant increase in speed on large double-precision arrays (on order of 10,000 elements). The matrix analysis functions det, rcond, hess, and expm also show significant increase in speed on large double-precision arrays.