Matrix functions¶
The group of matrix functions comprises functions for vector and matrix operations and also functions for sparse matrix handling. MATLAB has two storage modes, full and sparse. Only nonzero entries and their indices are stored for sparse matrices. Sparse matrices are not created automatically. But once initiated, sparsity propagates. Operations on sparse matrices produce sparse matrices and operations on a mixture of sparse and full matrices also normally produce sparse matrices.
The following functions are described in this chapter:
Vector and matrix operations
|
Special characters |
|
Special characters |
|
Create vectors and do matrix subscripting |
|
Matrix arithmetic |
|
Absolute value |
|
Matrix determinant |
|
Diagonal matrices and diagonals of a matrix |
|
Matrix inverse |
|
Vector length |
|
Maximum element(s) of a matrix |
|
Minimum element(s) of a matrix |
|
Generate a matrix of all ones |
|
Matrix dimensions |
|
Square root |
|
Sum of the elements of a matrix |
|
Generate a zero matrix |
Sparse matrix handling
|
Convert sparse matrix to full matrix |
|
Create sparse matrix |
|
Visualize sparsity structure |
Special characters [ ] ( ) = ‘ . , ;¶
- Purpose:
Special characters.
- Syntax:
[ ] ( ) = ' . , ;
- Description:
- [ ]
Brackets are used to form vectors and matrices.
- ( )
Parentheses are used to indicate precedence in arithmetic expressions and to specify an element of a matrix.
- =
Used in assignment statements.
- ‘
Matrix transpose.
X'is the transpose ofX. IfXis complex, the apostrophe sign performs complex conjugate as well. DoX.'if only the transpose of the complex matrix is desired.- .
Decimal point. 314/100, 3.14 and 0.314e1 are all the same.
- ,
Comma. Used to separate matrix subscripts and function arguments.
- ;
Semicolon. Used inside brackets to end rows. Used after an expression to suppress printing or to separate statements.
- Examples:
By the statement:
a = 2the scalar
ais assigned a value of 2. An element in a matrix may be assigned a value according to:A(2,5) = 3The statement:
D = [ 1 2 ; 3 4 ]results in matrix:
\[\begin{split}\mathbf{D} = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}\end{split}\]stored in the matrix bank. To copy the contents of the matrix
Dto a matrixE, use:E = DThe character ‘ is used in the following statement to store the transpose of the matrix
Ain a new matrixF:F = A'- Note:
These are MATLAB built-in characters.
: (colon operator)¶
- Purpose:
Create vectors and do matrix subscripting.
- Description:
The colon operator uses the following rules to create regularly spaced vectors:
- j : k
is the same as [
j,j + 1, ... , k]- j : i : k
is the same as [
j,j + i, j + 2i, ... , k]
The colon notation may also be used to pick out selected rows, columns, and elements of vectors and matrices:
- A( : , j )
is the
j:th column ofA- A( i , : )
is the
i:th row ofA
- Examples:
The colon ‘:’ used with integers:
d = 1:4results in a row vector:
\[\mathbf{d} = \begin{bmatrix} 1 & 2 & 3 & 4 \end{bmatrix}\]stored in the workspace.
The colon notation may be used to display selected rows and columns of a matrix on the terminal. For example, if we have created a 3-times-4 matrix
Dby the statement:D = [ d ; 2*d ; 3*d ]resulting in:
\[\begin{split}\mathbf{D} = \begin{bmatrix} 1 & 2 & 3 & 4 \\ 2 & 4 & 6 & 8 \\ 3 & 6 & 9 & 12 \end{bmatrix}\end{split}\]columns three and four are displayed by entering:
D( :, 3:4 )resulting in:
\[\begin{split}\mathbf{D}( :, 3:4 ) = \begin{bmatrix} 3 & 4 \\ 6 & 8 \\ 9 & 12 \end{bmatrix}\end{split}\]In order to copy parts of the
Dmatrix into another matrix the colon notation is used as:E( 3:4, 2:3 ) = D( 1:2, 3:4 )Assuming the matrix
Ewas a zero matrix before the statement is executed, the result will be:\[\begin{split}\mathbf{E} = \begin{bmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 3 & 4 & 0 \\ 0 & 6 & 8 & 0 \end{bmatrix}\end{split}\]- Note:
This is a MATLAB built-in character.
/ (arithmetic operators)
- Purpose:
Matrix arithmetic.
- Syntax:
A + B
A - B
A * B
A/s
- Description:
Matrix operations are defined by the rules of linear algebra.
- Examples:
An example of a sequence of matrix-to-matrix operations is:
D = A + B - CA matrix-to-vector multiplication followed by a vector-to-vector subtraction may be defined by the statement:
b = c - A * xand finally, to scale a matrix by a scalar
swe may use:B = A/s- Note:
These are MATLAB built-in operators.
abs¶
- Purpose:
Absolute value.
- Syntax:
B = abs(A)
- Description:
B = abs(A)computes the absolute values of the elements of matrixAand stores them in matrixB.- Examples:
Assume the matrix:
\[\begin{split}\mathbf{C} = \begin{bmatrix} -7 & 4 \\ -3 & -8 \end{bmatrix}\end{split}\]The statement:
D = abs(C)results in a matrix:
\[\begin{split}\mathbf{D} = \begin{bmatrix} 7 & 4 \\ 3 & 8 \end{bmatrix}\end{split}\]stored in the workspace.
- Note:
This is a MATLAB built-in function. For more information about the
absfunction, typehelp abs.
det¶
- Purpose:
Matrix determinant.
- Syntax:
a = det(A)
- Description:
a = det(A)computes the determinant of the matrixAand stores it in the scalara.- Note:
This is a MATLAB built-in function. For more information about the
detfunction, typehelp det.
diag¶
- Purpose:
Diagonal matrices and diagonals of a matrix.
- Syntax:
M = diag(v)
v = diag(M)
- Description:
For a vector
vwith n components, the statementM = diag(v)results in an n × n matrixMwith the elements ofvas the main diagonal.For a n × n matrix
M, the statementv = diag(M)results in a column vectorvwith n components formed by the main diagonal inM.- Note:
This is a MATLAB built-in function. For more information about the
diagfunction, typehelp diag.
full¶
- Purpose:
Convert sparse matrices to full storage class.
- Syntax:
A = full(S)
- Description:
A = full(S)converts the storage of a matrix from sparse to full. IfAis already full,full(A)returnsA.- Note:
This is a MATLAB built-in function. For more information about the
fullfunction, typehelp full.
inv¶
- Purpose:
Matrix inverse.
- Syntax:
B = inv(A)
- Description:
B = inv(A)computes the inverse of the square matrixAand stores the result in the matrixB.- Note:
This is a MATLAB built-in function. For more information about the
invfunction, typehelp inv.
length¶
- Purpose:
Vector length.
- Syntax:
n = length(x)
- Description:
n = length(x)returns the dimension of the vectorx.- Note:
This is a MATLAB built-in function. For more information about the
lengthfunction, typehelp length.
max¶
- Purpose:
Maximum element(s) of a matrix.
- Syntax:
b = max(A)
- Description:
For a vector
a, the statementb = max(a)assigns the scalarbthe maximum element of the vectora.For a matrix
A, the statementb = max(A)returns a row vectorbcontaining the maximum elements found in each column vector inA.The maximum element found in a matrix may thus be determined by
c = max(max(A)).- Examples:
Assume the matrix
Bis defined as:\[\begin{split}\mathbf{B} = \begin{bmatrix} -7 & 4 \\ -3 & -8 \end{bmatrix}\end{split}\]The statement:
d = max(B)results in a row vector:
\[\mathbf{d} = \begin{bmatrix} -3 & 4 \end{bmatrix}\]The maximum element in the matrix
Bmay be found bye = max(d)which results in the scalare = 4.- Note:
This is a MATLAB built-in function. For more information about the
maxfunction, typehelp max.
min¶
- Purpose:
Minimum element(s) of a matrix.
- Syntax:
b = min(A)
- Description:
For a vector
a, the statementb = min(a)assigns the scalarbthe minimum element of the vectora.For a matrix
A, the statementb = min(A)returns a row vectorbcontaining the minimum elements found in each column vector inA.The minimum element found in a matrix may thus be determined by
c = min(min(A)).- Examples:
Assume the matrix
Bis defined as:\[\begin{split}\mathbf{B} = \begin{bmatrix} -7 & 4 \\ -3 & -8 \end{bmatrix}\end{split}\]The statement:
d = min(B)results in a row vector:
\[\mathbf{d} = \begin{bmatrix} -7 & -8 \end{bmatrix}\]The minimum element in the matrix
Bis then found bye = min(d), which results in the scalare = -8.- Note:
This is a MATLAB built-in function. For more information about the
minfunction, typehelp min.
ones¶
- Purpose:
Generate a matrix of all ones.
- Syntax:
A = ones(m,n)
- Description:
A = ones(m,n)results in an \(m \times n\) array \(A\) with all ones.- Note:
This is a MATLAB built-in function. For more information about the
onesfunction, typehelp ones.
size¶
- Purpose:
Matrix dimensions.
- Syntax:
d = size(A)
[m,n] = size(A)
- Description:
d = size(A)returns a vector with two integer components,d = [m,n], from the matrixAwith dimensionsmtimesn.[m,n] = size(A)returns the dimensionsmandnof the m × n matrixA.- Note:
This is a MATLAB built-in function. For more information about the
sizefunction, typehelp size.
sparse¶
- Purpose:
Create sparse matrices.
- Syntax:
S = sparse(A)
S = sparse(m,n)
- Description:
S = sparse(A)converts a full matrix to sparse form by extracting all nonzero matrix elements. IfSis already sparse,sparse(S)returnsS.S = sparse(m,n)generates an m-times-n sparse zero matrix.- Note:
This is a MATLAB built-in function. For more information about the
sparsefunction, typehelp sparse.
spy¶
- Purpose:
Visualize matrix sparsity structure.
- Syntax:
spy(S)
- Description:
spy(S)plots the sparsity structure of any matrixS.Sis usually a sparse matrix, but the function also accepts full matrices and the nonzero matrix elements are plotted.- Note:
This is a MATLAB built-in function. For more information about the
spyfunction, typehelp spy.
sqrt¶
- Purpose:
Square root.
- Syntax:
B = sqrt(A)
- Description:
B = sqrt(A)computes the square root of the elements in matrixAand stores the result in matrixB.- Note:
This is a MATLAB built-in function. For more information about the
sqrtfunction, typehelp sqrt.
sum¶
- Purpose:
Sum of the elements of a matrix.
- Syntax:
b = sum(A)
- Description:
For a vector
a, the statementb = sum(a)results in a scalaracontaining the sum of all elements ofa.For a matrix
A, the statementb = sum(A)returns a row vectorbcontaining the sum of the elements found in each column vector ofA.The sum of all elements of a matrix is determined by
c = sum(sum(A)).- Note:
This is a MATLAB built-in function. For more information about the
sumfunction, typehelp sum.
zeros¶
- Purpose:
Generate a zero matrix.
- Syntax:
A = zeros(m,n)
- Description:
A = zeros(m,n)results in an \(m \times n\) array \(A\) of zeros.- Note:
This is a MATLAB built-in function. For more information about the
zerosfunction, typehelp zeros.