Matrix Subtraction Calculator

📚 Confused about how this is calculated? Read the full When Order Matters: The Subtle Rules of Operations →

Finding the Difference, Cell by Cell

Matrix subtraction mirrors addition exactly, just with the operation flipped: each entry in the second matrix is subtracted from the matching entry in the first. Order matters here in a way it doesn't for addition — A minus B is not the same result as B minus A.

The Formula

C = A − B (element-wise subtraction)
Cij = Aij − Bij

Worked Example

For A = [1, 2; 3, 4] and B = [5, 6; 7, 8]:

Matrix subtraction, cell by cell
PositionCalculationResult
Row 11−5, 2−6−4, −4
Row 23−7, 4−8−4, −4

Result matrix: [−4, −4; −4, −4].

Where This Matters

  • Change detection — subtracting a "before" data matrix from an "after" matrix isolates exactly what changed, cell by cell.
  • Error and residual analysis — comparing a predicted-values matrix against actual measurements to find the residual error matrix.
  • Image differencing — subtracting two frames of pixel data highlights motion or changes between them.

How to Use This Calculator

  1. Choose the matrix size: 2 x 2 Matrices or 3 x 3 Matrices.
  2. Enter each cell of Matrix A.
  3. Enter each cell of Matrix B, which will be subtracted from A.
  4. Select Calculate to get the difference matrix.

Related Calculations

Need to add instead? Use the Matrix Addition Calculator, or check either matrix's determinant with the Matrix Determinant Calculator.

Principles of Matrix Subtraction and Algebraic Properties

In linear algebra and multivariate analysis, matrix subtraction is the element-wise mathematical operation of subtracting each scalar entry of one matrix from the corresponding entry of another matrix of identical spatial dimensions. Matrix subtraction is fundamental in digital image difference processing, computer vision background subtraction, baseline drift correction in spectroscopy, and econometric financial variance tracking.

Conformability and the Element-Wise Formula

Matrix subtraction is defined exclusively for conformable matrices possessing identical row counts (m) and column counts (n). For two m×n matrices A and B:

C = A - B  ⇔  Cij = Aij - Bij  (for all 1 ≤ i ≤ m and 1 ≤ j ≤ n)

Fundamental Algebraic Properties of Matrix Subtraction

  • Non-Commutative: Reversing the order of subtraction negates the resulting matrix: A - B ≠ B - A = -(A - B).
  • Distributivity over Scalar Multiplication: c × (A - B) = (c × A) - (c × B).
  • Transpose Distributivity: (A - B)T = AT - BT.
  • Self-Subtraction (Zero Matrix): A - A = 0m×n.

Step-by-Step Worked Calculation Example

Example: Computer Vision Video Motion Detection via Background Subtraction

Problem: In a digital surveillance camera, a 2×3 pixel sensor array captures a static background calibration image matrix B and a current live video frame matrix F (pixel values 0 to 255):

Frame Matrix F = [ [185, 140, 95], [110, 220, 130] ]

Background Matrix B = [ [180, 138, 90], [105, 120, 128] ]

Calculate the motion difference matrix D = F - B to isolate moving objects.

Step 1: Compute element-wise subtraction for Row 1:

D11 = 185 - 180 = +5 (Noise / Static)

D12 = 140 - 138 = +2 (Static)

D13 = 95 - 90 = +5 (Static)

Step 2: Compute element-wise subtraction for Row 2:

D21 = 110 - 105 = +5 (Static)

D22 = 220 - 120 = +100 (Significant Motion Detected!)

D23 = 130 - 128 = +2 (Static)

Result Matrix D:
[ [5, 2, 5],
  [5, 100, 2] ]

Conclusion: The large difference at pixel coordinate (2,2) with Δ = 100 triggers the automated motion detection alarm.

Applications in Econometrics: Variance-Covariance Differences

In empirical econometrics, Hausman specification tests compute the difference matrix between fixed-effects (βFE) and random-effects (βRE) coefficient covariance matrices: Var(βFE - βRE) = Var(βFE) - Var(βRE), determining whether unobserved individual heterogeneity is correlated with regressors.

Residual Error Matrices in Ordinary Least Squares (OLS) Regression

In linear statistical modeling, the performance of a multivariate regression model is evaluated by computing the Residual Error Matrix (E) — the matrix subtraction of predicted target values (Ŷ) from actual observed target values (Y):

E = Y - Ŷ = Y - (X × β)

Where X is the design matrix of predictors and β is the parameter weight vector. The Sum of Squared Errors (SSE) is computed directly from the inner matrix product of the residual difference matrix: SSE = trace(ET × E), which the OLS optimization algorithm minimizes to find best-fit regression parameters.

Gaussian Mixture Models (GMM) in Video Background Subtraction

In advanced computer vision surveillance systems, real-world camera scenes contain swaying tree branches, shifting clouds, and water ripples that render simple static background subtraction ineffective.

Engineers implement Adaptive Gaussian Mixture Models (GMM): each pixel's color distribution across historical video frames is modeled as a mixture of K Gaussian probability density functions (typically K = 3 to 5).

Subtracting the adaptive probabilistic background matrix Bt from the incoming frame Ft isolates foreground moving targets while robustly filtering out natural environmental background luminance oscillations.

Spectroscopic Baseline Drift Subtraction

In analytical chemistry and Raman spectroscopy, raw spectral measurement matrices contain broad baseline fluorescence curves. Chemists apply matrix subtraction: Corrected Spectrum = Raw Spectrum Matrix - Estimated Baseline Matrix using Asymmetric Least Squares (AsLS) smoothing, resolving narrow analyte chemical absorption peaks.

Financial Balance Sheet Delta Analysis

In corporate financial accounting and forensic audit analysis, financial analysts calculate the change in financial condition across consecutive fiscal quarters using matrix subtraction: ΔB = BQ2 - BQ1, where matrix B contains financial statement asset, liability, and equity account balances. Evaluating the resulting delta matrix isolates quarter-over-quarter working capital trends, accounts receivable aging shifts, and cash flow anomalies.

Finite Difference Approximations in Computational PDE Solvers

In numerical simulation of heat transfer, fluid dynamics, and option pricing models (Black-Scholes PDE), spatial partial derivatives are discretized on spatial grids using Matrix Finite Differences. Subtracting adjacent node value matrices (D = Ui+1 - Ui) produces the discrete spatial gradient matrices required to time-step numerical simulation engines.

Medical MRI and CT Scan Image Subtraction Angiography (DSA)

In diagnostic radiology and vascular neurosurgery, Digital Subtraction Angiography (DSA) is the standard medical imaging technique used to visualize arterial blood vessels in high contrast.

A radiographic sensor captures a pre-contrast baseline image matrix (Mask Image) of the patient's skull and bones. Next, radiopaque iodine contrast dye is injected into the bloodstream and a second live image matrix is captured.

Executing real-time matrix subtraction (Angiogram Matrix = Live Image - Mask Image) mathematically eliminates all overlying dense radio-opaque bone and soft tissue structures, isolating the cerebral arterial blood vessels with microscopic clarity for catheter guidance.

Spatial Gradients and Sobel Filter Difference Operators

In computer vision edge detection, Sobel convolutional difference kernels subtract opposing adjacent pixel rows and columns, computing horizontal and vertical directional gradient matrices (∇Ix, ∇Iy) to outline object boundaries with subpixel accuracy.

Centering Matrices in Multivariate Statistics

In statistics, mean-centering data matrices uses matrix subtraction with projection matrices: Xc = X - (1/N) J X, subtracting column sample means prior to principal component analysis.