MATLAB - Array Division
MATLAB - Array and Matrix Division
Section titled “MATLAB - Array and Matrix 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.
Part 1: Element-Wise Division (./)
Section titled “Part 1: Element-Wise Division (./)”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.
Syntax
Section titled “Syntax”C = A ./ BEach element C(i,j) is calculated as A(i,j) / B(i,j).
Example: Dividing Two Matrices
Section titled “Example: Dividing Two Matrices”A = [8, 12, 10];B = [4, 3, 5];
% Perform element-wise divisionC = A ./ B
matrix1 = [9, 6; 15, 20];matrix2 = [3, 2; 5, 10];
resultMatrix = matrix1 ./ matrix2The output will be:
C = 2 4 2
resultMatrix = 3 3 3 2Common Pitfall: Dimension Mismatch
Section titled “Common Pitfall: Dimension Mismatch”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 rdivide Function
Section titled “The rdivide Function”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.
Part 2: Matrix Division (\ and /)
Section titled “Part 2: Matrix Division (\ and /)”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.
Left Division (\) for Solving Ax = b
Section titled “Left Division (\) for Solving Ax = b”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 \ bBest 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.
Example: Solving a System of Equations
Section titled “Example: Solving a System of Equations”Consider the system: 2x₁ + 3x₂ = 8 4x₁ + 1x₂ = 6
% Setup the system A*x = bA = [2, 3; 4, 1];b = [8; 6];
% Solve for x using left divisionx = A \ bMATLAB efficiently finds the solution:
x = 1.0000 2.0000This means x₁ = 1 and x₂ = 2.
Right Division (/) for Solving xA = B
Section titled “Right Division (/) for Solving xA = B”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 / AThis 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.
Example
Section titled “Example”A = [1, 2; 3, 4];B = [5, 8];
% Solve for x in x*A = Bx = B / AThe result is:
x = -2.0000 3.5000Summary: Which Division to Use?
Section titled “Summary: Which Division to Use?”- 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/.