Reduced Row Echelon Form (rref) Matrix in MATLAB


Reduced row echelon form (rref) matrix is a simplified matrix used to solve a system of linear equations. MATLAB provides some built-in functions to find the reduced row echelon form matrix of a given matrix. Read this article to learn these methods of finding the reduced row echelon form matrix of a given matrix using MATLAB. Before that let’s get an overview of rref matrix and its properties.

What is a Reduced Row Echelon Form Matrix?

In linear algebra, the reduced row echelon form matrix, also known as rref matrix, is a special and simplified matrix used to solve systems of linear equations. The rref matrix also useful in several other mathematical application, as it has several unique properties.

Some key properties of reduced row echelon form matrix are explained below −

  • In each row of a rref matrix, the left most non-zero element is known as a leading element and it is equal to 1.

  • In a rref matrix, all elements in the column above and below a leading element (1) are zeros.

  • In a rref matrix, a row whose all elements are zeros will be the bottom row.

  • In a rref matrix, each column which contains a leading element (1) has all other elements equal to 0.

  • In a rref matrix, the rows are ordered in a way that for any two non-zero rows, the leading element (1) in the lower row is to the right of the leading element (1) of the above row.

Let us take an example to understand how to find the reduced row echelon form matrix of a given matrix.

Consider a matrix A given below −

$$\mathrm{A \: = \: \begin{bmatrix}1 & 5 & 3 \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix}}$$

Here is the step-by-step process to find the reduced row echelon form matrix of A.

  • Step 1 − Obtain a leading element (1) in the first column. This can be done by multiplying the first row by 1 as follows:

$$\mathrm{\begin{bmatrix}1 \: \times 1 & 5 \: \times 1 & 3 \: \times 1 \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix} \: \rightarrow \begin{bmatrix}1 & 5 & 3 \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix}}$$

  • Step 2 − Reduce the elements below the leading 1 in the first column. To achieve this, we multiply the first row by -5 and add the result to the second row. Then, we multiply the first row by -9 and add the result to the third row, as shown below:

Multiplying 1st row to (-5) −

$$\mathrm{\begin{bmatrix}1 \: \times (-5) & 5 \: \times (-5) & 3 \: \times (-5) \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix} \: \rightarrow \begin{bmatrix}-5 & -25 & -15 \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix}}$$

Adding 1st row from the 2nd row −

$$\mathrm{\begin{bmatrix}-5 & -25 & -15 \\ 5 \: + (-5) & 6 \: + (-25) & 2 \: + (-15) \\ 9 & 8 & 5 \end{bmatrix} \rightarrow \begin{bmatrix}1 & 5 & 3 \\ 0 & -19 & -13 \\ 9 & 8 & 5 \end{bmatrix}}$$

Similarly, multiplying the 1st row by -9 and adding the result to the 3rd row, we get,

$$\mathrm{\begin{bmatrix}1 & 5 & 3 \\ 0 & -19 & -13 \\ 0 & -37 & -22 \end{bmatrix}}$$

  • Step 3 − Make the $\mathrm{2^{nd}}$ element of the $\mathrm{2^{nd}}$ column of the matrix obtained in the step (2) to a leading 1. We can achieve this by dividing the second row by (-19) as follows −

$$\mathrm{\begin{bmatrix}1 & 5 & 3 \\ \frac{0}{-19} & -\frac{19}{-19} & -\frac{13}{-19} \\ 0 & -37 & -22 \end{bmatrix} \rightarrow \begin{bmatrix}1 & 5 & 3 \\ 0 & 1 & \frac{13}{19} \\ 0 & -37 & -22 \end{bmatrix}}$$

  • Step 4 − Reduce the elements above and below the leading 1 in the $\mathrm{2^{nd}}$ column in the matrix obtained in the step (3).

    To make the above element 0, we multiply the $\mathrm{2^{nd}}$ row to (-5) and add the result to the $\mathrm{1^{st}}$ row.

    To make the below element 0, we multiply the $\mathrm{2^{nd}}$ row to 37 and add the result to the $\mathrm{3^{rd}}$ row.

    Hence, we get the following resulting matrix,

$$\mathrm{\begin{bmatrix}1 & 5 & 3 \\ 0 & 1 & \frac{13}{19} \\ 0 & -37 & -22 \end{bmatrix} \rightarrow \begin{bmatrix}1 & 0 & -\frac{8}{19} \\ 0 & 1 & \frac{13}{19} \\ 0 & 0 & \frac{63}{19} \end{bmatrix}}$$

  • Step 5 − Obtain the leading 1 in the third column of the matrix obtained in the step (4). We achieve this by dividing the $\mathrm{3^{rd}}$ row by (19/63) i.e.,

$$\mathrm{\begin{bmatrix}1 & 0 & -\frac{8}{19} \\ 0 & 1 & \frac{13}{19} \\ 0 & 0 & \frac{63}{19} \end{bmatrix} \rightarrow \begin{bmatrix}1 & 0 & -\frac{8}{19} \\ 0 & 1 & \frac{13}{19} \\ 0 & 0 & 1 \end{bmatrix}}$$

  • Step 6 − Reducing the elements to zero in the $\mathrm{3^{rd}}$ column above the leading 1.

    For this, we multiply the $\mathrm{3^{rd}}$ row by (19/8) and add the result to the $\mathrm{1^{st}}$ row, and multiply the $\mathrm{3^{rd}}$ row by (-19/13) and add the result to the $\mathrm{2^{nd}}$ row. We get the following resulting matrix.

$$\mathrm{\begin{bmatrix}1 & 0 & -\frac{8}{19} \\ 0 & 1 & \frac{13}{19} \\ 0 & 0 & 1 \end{bmatrix} \rightarrow \begin{bmatrix}1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}}$$

This is the reduced row echelon form matrix of the matrix A.

This is all about basics of reduced row echelon form (rref) matrix. Let us now discuss how to find the rref matrix of a given matrix using MATLAB.

How to Find Reduced Row Echelon Form (rref) Matrix in MATLAB?

MATLAB provides a built-in function "rref" that we can use to obtain the reduced row echelon form (rref) matrix of a given matrix.

The "rref" function can have the following syntax formats −

  • R = rref(A);

  • R = rref(A, tol);

  • [R, p] = rref(A);

Let us discuss these functions in detail with the help of examples in MATLAB programming.

Find Reduced Row Echelon Form Matrix

As mentioned above, in MATLAB, we can use the "rref" function to find the reduced row echelon form matrix R of a given matrix A.

Here are the steps involved in finding the rref matrix using MATLAB −

  • Step 1 − Provide the input matrix A.

  • Step 2 − Use the "rref" function to find the rref matrix of the matrix A.

  • Step 3 − Display the results using the "disp" function.

Example 1

Let us take an example to understand this process.

% MATLAB code to find rref matrix
% Create a sample matrix
A = [1, 5, 3; 5, 6, 2; 9, 8, 5];

% Obtain the reduced row echelon form matrix
R = rref(A);

% Display the original and rref matrices
disp('Original Matrix A:');
disp(A);
disp('Reduced Row Echelon Form Matrix R:');
disp(R);

Output

It will produce the following output −

Original Matrix A:
     1     5     3
     5     6     2
     9     8     5
Reduced Row Echelon Form Matrix R:
     1     0     0
     0     1     0
     0     0     1

Find Reduced Row Echelon Form Matrix with Specified Tolerance

In MATLAB, we can use the "rref" function with specified tolerance for rank determination of the matrix. The code implementation of this function remains the same as the previous.

Example 2

The following example shows the process of finding rref matrix with a specified tolerance.

% MATLAB code to find rref matrix with specified tolerance
% Create a sample matrix
A = [2, 9, 3; 4, 6, 1; 9, 7, 5];

% Specify a tolerance value desired
tol = 1e-7;

% Obtain the rref matrix with tolerance
R = rref(A, tol);

% Display the original matrix and rref matrix
disp('Original Matrix A:');
disp(A);
disp('Reduced Row Echelon Form Matrix R:');
disp(R);

Output

It will produce the following output:

Original Matrix A:
   2     9     3
   4     6     1
   9     7     5
Reduced Row Echelon Form Matrix R:
   1     0     0
   0     1     0
   0     0     1

Find Reduced Row Echelon Form Matrix along with Pivot Columns

In MATLAB, we can also use the "rref" function to find the reduced row echelon form matrix and a vector containing the indices of pivot columns of the rref matrix.

Example 3

The following example shows the code implementation to find the rref matrix and pivot columns using MATLAB.

% MATLAB code to find rref matrix and pivot columns
% Create a sample matrix
A = [3, 1, 5; 2, 6, 2; 7, 1, 4];

% Find the rref matrix and pivot columns
[R, p] = rref(A);	% R = rref matrix, p = pivot columns

% Display the original matrix, rref matrix, and pivot columns
disp('Original Matrix A:');
disp(A);
disp('Reduced Row Echelon Form Matrix R:');
disp(R);
disp('Pivot Columns p:');
disp(p);

Output

It will produce the following output:

Original Matrix A:
   3     1     5
   2     6     2
   7     1     4
Reduced Row Echelon Form Matrix R:
   1     0     0
   0     1     0
   0     0     1
Pivot Columns p:
   1     2     3

Let us now discuss that how to use the reduced row echelon form matrix to solve a system of linear equations in MATLAB.

Solve System of Equations using Reduced Row Echelon Form Matrix in MATLAB

To understand how we can use the rref matrix to solve a system of linear equations, let us take an example.

Consider we have a system of linear equations with 3 equations and 3 unknowns, given below.

$$\mathrm{a \: + \: 3b \: + \: 5c \: = \: 10}$$

$$\mathrm{5a \: + \: 4b \: + \: c \: = \: 2}$$

$$\mathrm{7a \: + \: 5b \: + \: 10c \: = \: 7}$$

First of all, we create an augmented matrix for the given system of equations, given below.

A = [1 3 5; 5 4 1; 7 5 10];	% Left-hand side array of coefficients
b = [10; 2; 7];	% Right-hand side vector

The augmented matrix will be,

augmented_mat = [A, b];

Then, we will find the rref matrix of this augmented matrix and solve it to find the values of unknowns.

Example 4

Let us understand this process with the help of an example in MATLAB programming.

% MATLAB code to solve linear equations using rref matrix
% Provide the matrix of LHS coefficients
A = [1 3 5; 5 4 1; 7 5 10];

% Provide the vector of RHS constants
b = [10; 2; 7];

% Create the augmented matrix for the given system
augmented_mat = [A, b];

% Obtain the reduced row echelon form matrix of augmented matrix
R = rref(augmented_mat);

% Use the rref matrix to find the solution of equations
S = R(:, end);

% Display the augmented matrix, rref matrix, and solution matrix
disp('Augmented Matrix:');
disp(augmented_mat);
disp('Reduced Row Echelon Form Matrix:');
disp(R);
disp('Solution Matrix:');
disp(S);

Output

It will produce the following output:

Augmented Matrix:
   1     3     5    10
   5     4     1     2
   7     5    10     7
Reduced Row Echelon Form Matrix:
   1.0000        0         0    -2.0275
        0   1.0000         0     2.8624
        0        0    1.0000     0.6881
Solution Matrix:
   -2.0275
   2.8624
   0.6881

This example demonstrates how to solve a given system of linear equations using the reduced row echelon form matrix in MATLAB.

Conclusion

In conclusion, the reduced row echelon form (rref) matrix is a simplified form of matrix used to find the rank of a matrix or to solve a system of linear equations. MATLAB provides a built-in function "rref" that we can use to find the rref matrix of a given matrix.

In this article, I explained the methods of finding reduced row echelon form matrix using the "rref" function in MATLAB. Also, I have explained the process using a rref matrix to solve a system of linear equations.

Updated on: 26-Oct-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements