Skip to content

MATLAB - Array Division

In MATLAB, the term ‘division’ can refer to two distinct operations: element-wise division and matrix division. Understanding the difference is crucial for correct and efficient numerical computing. This tutorial clarifies both types of division and their corresponding operators.

Element-wise division, also known as array division, operates on corresponding elements of two arrays. It is performed using the ./ (dot-slash) operator. Both arrays must have the same dimensions, or one must be a scalar.

C = A ./ B

Each element C(i,j) is calculated as A(i,j) / B(i,j).

A = [8, 12, 10];
B = [4, 3, 5];
% Perform element-wise division
C = A ./ B
matrix1 = [9, 6; 15, 20];
matrix2 = [3, 2; 5, 10];
resultMatrix = matrix1 ./ matrix2

The output will be:

C =
2 4 2
resultMatrix =
3 3
3 2

Attempting to use ./ on two non-scalar arrays of different sizes will result in an error. Ensure your arrays are correctly sized for the operation.

The function rdivide(A, B) is the functional equivalent of A ./ B. It is less commonly used for direct calculations but can be useful for passing the division operation as a function handle.

Matrix division is a completely different concept related to solving systems of linear equations. It does not perform element-by-element division. There are two types: left and right matrix division.

The backslash operator \ is one of the most powerful and important operators in MATLAB. It is used to solve the linear system A*x = b for the unknown vector or matrix x.

x = A \ b

Best Practice: The Backslash is Best! For solving linear systems, x = A \ b is significantly faster and more numerically stable than calculating the inverse and multiplying (x = inv(A) * b). Always prefer the backslash operator.

Consider the system: 2x₁ + 3x₂ = 8 4x₁ + 1x₂ = 6

% Setup the system A*x = b
A = [2, 3; 4, 1];
b = [8; 6];
% Solve for x using left division
x = A \ b

MATLAB efficiently finds the solution:

x =
1.0000
2.0000

This means x₁ = 1 and x₂ = 2.

The forward slash operator / performs right matrix division. x = B / A is the solution to the linear system x*A = B for the unknown x.

x = B / A

This is mathematically equivalent to x = (A' \ B')'. It is used less frequently than left division but is essential in certain control theory and signal processing contexts.

A = [1, 2; 3, 4];
B = [5, 8];
% Solve for x in x*A = B
x = B / A

The result is:

x =
-2.0000 3.5000
  • To divide corresponding elements of two arrays: Use element-wise division ./.
  • To solve a system of linear equations of the form A*x = b: Use left matrix division \.
  • To solve a system of linear equations of the form x*A = B: Use right matrix division /.