13.10: Special Matrices and Matrix Operations

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    13.10: Special Matrices and Matrix Operations

    A matrix is a two-dimensional array of numbers. A matrix with m rows and n columns is called an m by n matrix.

    Mat1 = [5 6 7; 2 6 1]

    This matrix has 2 rows and 3 columns, so it is a 2 by 3 matrix. The semicolon separates the first row from the second row.

    size(Mat1)

    The size function returns the dimensions of the matrix. This is useful when you are checking whether two matrices can be added, subtracted, or multiplied.

    Square Matrices

    A square matrix has the same number of rows and columns. For example, this is a 3 by 3 square matrix:

    squareMat = [2 6 5; 6 11 3; 6 4 0]

    Square matrices are especially important because determinants and inverses are defined only for square matrices.

    Transpose of a Matrix

    The transpose of a matrix switches its rows and columns. In MATLAB, you can use the apostrophe operator or the transpose function.

    Example \(\PageIndex{1}\)

    Transpose operations.

    mat1 = [4 3 1; 2 5 6]
    
    transmat1 = mat1'
    
    transmat2 = transpose(mat1)
    Solution
    mat1 =
    
       4   3   1
       2   5   6
    
    transmat1 =
    
       4   2
       3   5
       1   6
    
    transmat2 =
    
       4   2
       3   5
       1   6

    The results transmat1 and transmat2 should be the same for real-valued matrices.

    Note

    For real numbers, the apostrophe operator works like transpose. For complex numbers, the apostrophe operator computes the complex conjugate transpose. For a regular transpose of complex data, use .' instead.

    Diagonal Matrices

    The main diagonal of a matrix runs from the upper-left corner to the lower-right corner. MATLAB has several useful functions for working with this diagonal. For example, in a 3-by-3 matrix, the main diagonal contains the elements in positions (1,1), (2,2), and (3,3).

    In MATLAB, the diag function can be used to extract the main diagonal of a matrix.

    mat4 = [4 3 1; 
            2 5 6; 
            9 4 1];
    diagValues = diag(mat4)
    

    The result is:

    diagValues = [4; 5; 1]

    These are the elements on the main diagonal.

    Identity Matrix

    An identity matrix is a square matrix with ones on the main diagonal and zeros everywhere else. For example, a 3-by-3 identity matrix is:

    [1 0 0; 
    0 1 0; 
    0 0 1]

    The identity matrix is important because it behaves like the number 1 in matrix multiplication. For regular numbers, multiplying by 1 does not change the value:

    5 * 1 = 5

    Similarly, multiplying a matrix by an identity matrix does not change the matrix, as long as the dimensions are compatible.

    In MATLAB, we create an identity matrix using the eye function.

    identity3 = eye(3)

    This creates a 3-by-3 identity matrix.

    You can also create a rectangular matrix with ones on the main diagonal using:

    identity3by4 = eye(3,4)

    This creates a 3-by-4 matrix with ones along the main diagonal and zeros elsewhere.

    Trace of a Matrix

    The trace of a square matrix is the sum of the elements on its main diagonal.

    For example, for the matrix:

    mat4 = [4 3 1; 2 5 6; 9 4 1];

    the main diagonal values are:

    4, 5, and 1

    So the trace is:

    4 + 5 + 1 = 10

    In MATLAB, the trace function calculates this directly:

    diagonalSum = trace(mat4)

    The result is:

    diagonalSum = 10

    The trace is only defined for square matrices in linear algebra. It is used in several areas of engineering, mathematics, and data analysis, especially when working with matrix properties and transformations.

    Lower and Upper Triangular Matrices

    A triangular matrix is a square matrix where all the values on one side of the main diagonal are zero. Triangular matrices are useful in linear algebra because they make many matrix calculations easier, especially when solving systems of linear equations.

    There are two main types of triangular matrices:

    A lower triangular matrix has zeros above the main diagonal.

    An upper triangular matrix has zeros below the main diagonal.

    For example, this is a lower triangular matrix:

    L = [4 0 0; 
         2 5 0; 
         9 4 1]

    Notice that all entries above the main diagonal are zero.

    This is an upper triangular matrix:

    U = [4 3 1; 
         0 5 6; 
         0 0 1]

    Notice that all entries below the main diagonal are zero.

    MATLAB can extract the lower or upper triangular part of a matrix using the tril and triu functions.

    For example:

    mat4 = [4 3 1; 2 5 6; 9 4 1];
    lowerPart = tril(mat4)
    

    The result is:

    lowerPart = [4 0 0; 2 5 0; 9 4 1]

    MATLAB keeps the main diagonal and the values below it. The values above the main diagonal are replaced with zeros.

    Similarly:

    upperPart = triu(mat4)

    The result is:

    upperPart = [4 3 1; 0 5 6; 0 0 1]

    MATLAB keeps the main diagonal and the values above it. The values below the main diagonal are replaced with zeros.

    Note

    Triangular matrices are important because many numerical methods solve systems of equations by converting a matrix into triangular form behind the scenes.

    Symmetric matrix

    A symmetric matrix is a square matrix that is equal to its transpose. In other words a matrix A is symmetric if \(A = A'\).

    For example:

    A = [2 5 7; 
         5 3 1; 
         7 1 4]

    is symmetric because the element in row 1, column 2 is equal to the element in row 2, column 1, the element in row 1, column 3 is equal to the element in row 3, column 1, and so on.

    In MATLAB, you can check whether a matrix is symmetric using:

    isequal(A, A')

    or, for numerical calculations where small round-off errors may occur:

    issymmetric(A)

    A symmetric matrix must be square. So, a rectangular matrix cannot be symmetric.

    Example \(\PageIndex{1}\)

    Matrix Operations.

    mat4 = [4 3 1; 2 5 6; 9 4 1]
    
    
    % return the elements on the main diagonal
    diagValues = diag(mat4)
    
    % create a 3 by 3 identity matrix
    identity3 = eye(3)
    
    % create a 3-by-4 identity matrix
    identity3by4 = eye(3,4)
    
    % find the sum of the elements on the diagonal
    diagonalSum = trace(mat4)
    
    % return the lower triangular matrix
    lowerMatrix = tril(mat4)
    
    % return the upper triangular matrix
    upperMatrix = triu(mat4)
    Solution
    mat4 =
    
       4   3   1
       2   5   6
       9   4   1
    
    diagValues =
    
       4
       5
       1
    
    identity3 =
    
    Diagonal Matrix
    
       1   0   0
       0   1   0
       0   0   1
    
    identity3by4 =
    
    Diagonal Matrix
    
       1   0   0   0
       0   1   0   0
       0   0   1   0
    
    diagonalSum = 10
    lowerMatrix =
    
       4   0   0
       2   5   0
       9   4   1
    
    upperMatrix =
    
       4   3   1
       0   5   6
       0   0   1
    

    This page titled 13.10: Special Matrices and Matrix Operations was last modified on Tue, 30 Jun 2026 02:11:59 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.