# 11.6: Common Matrix Functions

$$\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 11.6.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 11.6.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 11.6.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 could “nest” the functions as max(max(A)), which would give us 8.

Or, we can use max() with the colon operator to treat the entire matrix as a vector: max(A(:)), which would also compute 8. [C.A.S.]

## 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 11.6.4

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

>> sum(A)

ans =
6 8 10 12

(the column sums

and

>> sum(A,2)% The "2" means sum in the 2nd dimension, that is, across the rows

ans =
10
26

(the row sums)

.

## Exercise

##### Exercise $$\PageIndex{1}$$ "Hilbert matrix" operations

Consider the 5 x 5 "Hilbert matrix"
H5 = [1 1/2 1/3 1/4 1/5;
1/2 1/3 1/4 1/5 1/6;
1/3 1/4 1/5 1/6 1/7;
1/4 1/5 1/6 1/7 1/8;
1/5 1/6 1/7 1/8 1/9];

a. Using a command in this section, find the dimensions of H5.
b. Using commands in this section, find the column sums of H5 and the row sums of H5.
c. Use the max() function to determine the value of the maximum entry of H5.