Matlab

How Does Matrix Division Work in MATLAB

Matrix division plays a vital role in MATLAB when it comes to solving linear systems, performing element-wise division, and conducting numerical computations. In this article, we will explore four important matrix division functions in MATLAB: mldivide, rdivide, ldivide, and mrdivide.

How does Matrix Division Works in MATLAB

Matrix division in MATLAB is a bit different from regular division. When you divide two matrices, MATLAB actually performs element-wise division. This means that each element in the first matrix is divided by the corresponding element in the second matrix and here are some ways for dividing two matrices in MATLAB:

1: mldivide (A \ B)
The mldivide function, represented by the backslash operator (\), is utilized for solving linear systems of equations. It finds the solution vector X that satisfies the equation A * X = B. The mldivide function automatically adjusts the method of solution based on the properties of the input matrices.

A = [1 2; 3 4];
B = [5; 6];
X = A \ B;
disp(X);

Output

2: rdivide (A ./ B)
The rdivide function, indicated by the dot division operator (./), conducts element-wise division between two matrices A and B. It divides each element in matrix A by the corresponding element in matrix B, generating a new matrix with dimensions matching the original matrices.

A = [10 20; 30 40];
B = [2 4; 5 10];
result = A ./ B;
disp(result);

Output

3: ldivide (A .\ B)
The ldivide function, represented by the dot backslash operator (.\), conducts element-wise division in the opposite order of rdivide. It calculates the division of each element in matrix B by the corresponding element in matrix A, resulting in a new matrix with dimensions matching the input matrices.

A = [1 2; 3 4];
B = [10 20; 30 40];
result = B .\ A;
disp(result);

Output

4: mrdivide (A / B)
The mrdivide function, denoted by the forward slash operator (/), performs matrix right division. It is used to solve linear systems of equations where the right-hand side matrix is divided by the left-hand side matrix. The result is the solution matrix X that satisfies the equation X * A = B.

A = [1 2; 3 4];
B = [5 6; 7 8];
X = B / A;
disp(X);

Output

Note: If the output is displaying a “-“, it means that the linear system does not have a unique solution, or it is inconsistent, meaning there is no solution that satisfies all the equations simultaneously.

Conclusion

Matrix division in MATLAB provides powerful tools for solving linear systems, performing element-wise division, and conducting numerical computations. By using mldivide, rdivide, ldivide, and mrdivide functions, you can efficiently handle complex computations and tackle a wide range of problems.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.