Skip to main content
Engineering LibreTexts

2.4: Common Matrix Functions

  • Page ID
    14918
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Commands: size and length

    Many functions involving matrices will only work if the dimensions of the matrices satisfy certain conditions. The size(M) command returns the number of rows and columns in M.

    Example 2.4.1

    >> A = [1 2 3 4;5 6 7 8];size(A)

    ans =
    2 4

    and

    >> B = [1; 6; 0; 9];size(B)

    ans =
    4 1

    The size command is also what is known as an “overloaded” function in that it can be used in a couple of ways. Suppose we only wanted to know only the number of rows in a matrix M. We could find the size(M), store this as a variable and then select the first entry. Instead, the size command takes a second entry that will allow us to get what we want.

    Example 2.4.2

    >> A = [1 2 3 4;5 6 7 8];size(A,1)

    ans =
    2

    (the number of rows)

    and

    >> A = [1 2 3 4;5 6 7 8];size(A,2)

    ans =
    4

    (the number of columns).

    The length of a vector (either a row or column) is simply the number of elements in the vector:

    Example 2.4.3

    >> c=1:5;

    >> length(c)

    ans =
    5

    However, the “length of a matrix” is defined in Matlab as the larger of the number of rows and the number of columns. That is, length(A) is equivalent to max(size(A)).

    Commands: max and min

    These two functions are (almost) self explanatory. For the matrix that we are using, A = [1 2 3 4;5 6 7 8], if we try max(A), we get 5 6 7 8. What’s going on?

    Remember that Matlab works on column precedence so that what max is doing is not finding the maximum value of the entire matrix, but instead finding the maximums of each column. The only exception occurs when the starting matrix is either a row or column vector. For example, for B = [1; 6; 0; 9], max(B) does give us 9 (the largest value in B).

    So, to get the largest element in A, we would have to “nest” the functions as max(max(A)), which would give us 8.

    Commands: sum and prod

    Once again, these seem reasonably named functions (see previous bullet). And once again, they return not the sum\product of every entry in matrix, but the column sums\products with the exception being for vectors, in which case you do get the sum\product of every entry in the vector. But, what if you want to get the sum along the rows? Well, once again sum is an overloaded operator and we can use:

    Example 2.4.4

    >> A = [1 2 3 4;5 6 7 8]

    >> sum(A)

    ans =
    6 8 10 12

    (the column sums

    and

    >> sum(A,2)

    ans =
    10
    26

    (the row sums)

    For those that know more linear algebra, we list some familiar commands.

    Commands: cross and dot

    These are the functions to find the cross product or dot product of two vectors using dot(v1,v2) and cross(v1,v2). A few things to note. First, the vectors both have to be the same length, but it doesn’t matter if they are both row vectors, both column vectors, or even one of each. Second, for the cross product, recall that you need them both to be of length 3 (i.e. each of dimension 1 × 3 or 3 × 1).


    Commands: det and inv

    For a square matrix S, you can find the determinant and inverse using the commands det(S) and inv(S). You can also use the command S∧(-1) although a common mistake is to forget the parentheses around the −1.

    Command: eye

    The folks at Mathworks do have an interesting sense of humor. To create the 5×5 identity matrix, for example, you could type all 25 entries of ones and zeros, or you can use the command eye(5). Get it? “eye”? Get it? Nevermind.


    This page titled 2.4: Common Matrix Functions is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Troy Siemers (APEX Calculus) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.