Day 4

RREF and Sorting in Matlab

Make one pivot in the matrix \(A\):

 

1. Find the nonzero row with the smallest index, call it \(r\).

2. In row \(r\) find the nonzero entry with the smallest index, call it \(c\).

3. Multiply Row \(r\) by \(\dfrac{1}{A(r,c)}\).

In Matlab:

\[A(r,:)=1/A(r,c)\ast A(r,:);\]

4. For each \(s\neq r\) we replace Row \(s\) with

\[(\text{Row}\ s) - A(s,c)\cdot(\text{Row}\ r)\]

In Matlab:

\[A(s,:) = A(s,:) - A(s,c)\ast A(r,:);\]

A(r,:) = 1/A(r,c)*A(r,:);
for s=1:m
	if s~=r
		A(s,:) = A(s,:)-A(s,c)*A(r,:);
	end
end

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 5\\ 2\\ -1\\ 0\\ 3\\ -3\end{array}\right]\]

We want to sort the entries so that they are ascending, but how do we get Matlab to do it?

Is \(-3\leq 5\)?

Yes! So we do nothing

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 5\\ 2\\ -1\\ 0\\ 3\\ -3\end{array}\right]\]

Is \(5\leq 2\)?

No! So we

swap them

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 5\\ 2\\ -1\\ 0\\ 3\\ -3\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ 5\\ -1\\ 0\\ 3\\ -3\end{array}\right]\]

Is \(5\leq 2\)?

No! So we

swap them

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ 5\\ -1\\ 0\\ 3\\ -3\end{array}\right]\]

Is \(5\leq -1\)?

No! So we

swap them

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 5\\ 0\\ 3\\ -3\end{array}\right]\]

Is \(5\leq -1\)?

No! So we

swap them

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 5\\ 0\\ 3\\ -3\end{array}\right]\]

Is \(5\leq 0\)?

No! So we

swap them

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 0\\ 5\\ 3\\ -3\end{array}\right]\]

Is \(5\leq 0\)?

No! So we

swap them

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 0\\ 5\\ 3\\ -3\end{array}\right]\]

Is \(5\leq 3\)?

No! So we

swap them

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 0\\ 3\\ 5\\ -3\end{array}\right]\]

Is \(5\leq 3\)?

No! So we

swap them

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 0\\ 3\\ 5\\ -3\end{array}\right]\]

Is \(5\leq -3\)?

No! So we

swap them

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 0\\ 3\\ -3\\ 5\end{array}\right]\]

Is \(5\leq -3\)?

No! So we

swap them

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 0\\ 3\\ -3\\ 5\end{array}\right]\]

Are we done?

No, but how does Matlab know we aren't done?

We are done when we look at consecutive pair, and all of them are already in the right order.

That is, when we do a pass through the list and do no swaps.

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 0\\ 3\\ -3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ 2\\ -1\\ 0\\ 3\\ -3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 2\\ 0\\ 3\\ -3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 2\\ 0\\ 3\\ -3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ 2\\ 3\\ -3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ 2\\ 3\\ -3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ 2\\ 3\\ -3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ 2\\ -3\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ 2\\ -3\\ 3\\ 5\end{array}\right]\]

Done?

No!

(We did at least one swap)

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ 2\\ -3\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ 2\\ -3\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ 2\\ -3\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ 2\\ -3\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ -3\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ -3\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ -3\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ -3\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ -3\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ 0\\ -3\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ -3\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ -3\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ -3\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ -3\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ -3\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -1\\ -3\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Done?

No!

The numbers are in the correct order, but how does Matlab know?

So we do another pass.

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Sorting a list

Example. Consider the list column vector

\[x=\left[\begin{array}{r}-3\\ -3\\ -1\\ 0\\ 2\\ 3\\ 5\end{array}\right]\]

Done?

Yes!

On the last pass we didn't do any swaps, so ever pair of consecutive entries is in the correct order.

Bubble sort algorithm

Let \(x\in\R^{n}\), that is, \(x\) is an \(n\times 1\) vector. We wish to write the entries of \(x\) in ascending order.

For each \(r\) in \(\{1,2,\ldots,n-1\}\). Look at \(x(r)\) and \(x(r+1)\). If \(x(r)>x(r+1)\), then swap \(x(r)\) and \(x(r+1)\).

Matlab:

 

for r=1:(n-1)
	if x(r)>x(r+1)
		y=x;
    		x(r)=y(r+1);
    		x(r+1)=y(r);
 	end
 end

If any swaps were done in that for loop, then do it again. If no swaps were done, then your list is sorted.

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 0 & 1 & 0 & 6 & 0\\ 0 & 0 & 0 & 0 & 0\\ 1 & 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 0 & 1\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 2\\ 6\\ 1\\ 5\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 0 & 1 & 0 & 6 & 0\\ 0 & 0 & 0 & 0 & 0\\ 1 & 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 0 & 1\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 2\\ 6\\ 1\\ 5\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 0 & 1 & 0 & 6 & 0\\ 0 & 0 & 0 & 0 & 0\\ 1 & 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 0 & 1\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 2\\ 6\\ 1\\ 5\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 0 & 1 & 0 & 6 & 0\\ 1 & 0 & 0 & 3 & 0 \\ 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 2\\ 1\\ 6\\ 5\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 0 & 1 & 0 & 6 & 0\\ 1 & 0 & 0 & 3 & 0 \\ 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 2\\ 1\\ 6\\ 5\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 0 & 1 & 0 & 6 & 0\\ 1 & 0 & 0 & 3 & 0 \\ 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 2\\ 1\\ 5\\ 6\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 0 & 1 & 0 & 6 & 0\\ 1 & 0 & 0 & 3 & 0 \\ 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 2\\ 1\\ 5\\ 6\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 1 & 0 & 0 & 3 & 0 \\ 0 & 1 & 0 & 6 & 0\\ 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 1\\ 2\\ 5\\ 6\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 1 & 0 & 0 & 3 & 0 \\ 0 & 1 & 0 & 6 & 0\\ 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 1\\ 2\\ 5\\ 6\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 1 & 0 & 0 & 3 & 0 \\ 0 & 1 & 0 & 6 & 0\\ 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 1\\ 2\\ 5\\ 6\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 1 & 0 & 0 & 3 & 0 \\ 0 & 1 & 0 & 6 & 0\\ 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 1\\ 2\\ 5\\ 6\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 1 & 0 & 0 & 3 & 0 \\ 0 & 1 & 0 & 6 & 0\\ 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 1\\ 2\\ 5\\ 6\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

Sorting Rows

Assume we have a matrix \(A\) where each row of \(A\) either contains a pivot or is the zero row.

 

Example:

\[\begin{bmatrix} 1 & 0 & 0 & 3 & 0 \\ 0 & 1 & 0 & 6 & 0\\ 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0\end{bmatrix}\]

How do we sort these rows so that we get a row equivalent matrix in RREF?

\[\begin{bmatrix} 1\\ 2\\ 5\\ 6\end{bmatrix}\] 

Define a column vector, where the entry in row \(r\) is the column containing the pivot of row \(r\) in \(A\)

In RREF

What about the elementary matrices

Assume the matrix we are row reducing is \(m\times n\). Then elementary matrices are \(m\times m\).

1) Swap rows \(r\) and \(s\):

Example: If \(m=6\) and we want to swap rows \(2\) and \(4\) then the elementary matrix is:

\[E=\begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 0 & 0 & 1\end{bmatrix}\]

E = eye(m);
E(r,r)=0;
E(s,s)=0;
E(r,s)=1;
E(s,r)=1;

Matlab:

What about the elementary matrices

Assume the matrix we are row reducing is \(m\times n\). Then elementary matrices are \(m\times m\).

2) Multiply row \(r\) by the nonzero scalar \(a\)

Example: If \(m=6\) and we want to multiply row \(2\) by \(\frac{1}{2}\)

\[E=\begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0\\ 0 & \frac{1}{2} & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 0 & 0 & 1\end{bmatrix}\]

E = eye(m);

E(r,r) = a;

Matlab:

What about the elementary matrices

Assume the matrix we are row reducing is \(m\times n\). Then elementary matrices are \(m\times m\).

3) Replace Row \(r\) with \((\text{Row}\ r) + a(\text{Row}\ s)\)

Example: If \(m=6\) and we want to Row \(3\) with \((\text{Row}\ 3) + (-2)(\text{Row}\ 6)\)

\[E=\begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & -2\\ 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 0 & 0 & 1\end{bmatrix}\]

E = eye(m);

E(r,s) = a;

Matlab:

Linear Algebra Day 4

By John Jasper

Linear Algebra Day 4

  • 321