03 Matrix vs Element-by-Element Arithmetic

Overview: Algebraic Math vs. Vectorized Math

MATLAB features two distinct mathematical systems: Matrix Arithmetic (which treats arrays as whole linear systems) and Element-by-Element Arithmetic (which applies operations element-wise).


1. Matrix Operators vs. Element-wise Operators

Operators preceded by a period (.) run element-wise, whereas standard operators perform matrix linear algebra operations.

OperatorTypeMath DescriptionDimension Requirements
A * BMatrixTraditional dot product matrix multiplication.Inner dimensions must match:
A .* BElement-wiseMultiplies individual elements: .Dimensions of and must match exactly (or broadcast)
A / BMatrixRight division: equivalent to .Column count of must equal column count of
A ./ BElement-wiseDivides individual elements: .Dimensions of and must match exactly
A ^ nMatrixMatrix power: multiplies by itself times (). must be a square matrix ()
A .^ nElement-wiseRaises each individual element to power .Any dimension shape allowed

2. Element-wise Functions vs. Matrix Functions (m suffix)

MATLAB has parallel function suites for scalar and matrix math. Functions ending with m operate on the matrix as a whole system.

Dynamic Comparisons:

Exponentiation:

  • exp(A)
    • Syntax: Y = exp(A)
    • Behavior: Computes for every element.
  • expm(A)
    • Syntax: Y = expm(A)
    • Behavior: Computes the matrix exponential via Taylor series expansions:

Square Root:

  • sqrt(A)
    • Syntax: Y = sqrt(A)
    • Behavior: Computes the square root of each individual element.
  • sqrtm(A)
    • Syntax: X = sqrtm(A)
    • Behavior: Computes the matrix square root, finding a matrix such that .

💻 Conceptual Code Demonstration

% 1. Create a Matrix
A = [1 2; 3 4];
B = [2 0; 1 2];
 
% 2. Compare Multiplications
matrixMul = A * B;   % Result: [4 4; 10 8] (Matrix multiplication)
elementMul = A .* B; % Result: [2 0; 3 8] (Element-by-element multiplication)
 
% 3. Compare Powers
matrixPower = A ^ 2;   % Result: [7 10; 15 22] (A * A)
elementPower = A .^ 2; % Result: [1 4; 9 16] (Element-by-element squaring)
 
% 4. Matrix Exponential vs Element Exponential
elementExp = exp(A);  % Calculates exp(1), exp(2), etc.
matrixExp = expm(A);  % Calculates e^A using matrix algebra