MATLAB - Array Multiplication
MATLAB - Array Operations: Element-Wise vs. Matrix Multiplication
Section titled “MATLAB - Array Operations: Element-Wise vs. Matrix Multiplication”In MATLAB, the term ‘multiplication’ can have two distinct meanings, and understanding the difference is crucial for correct and efficient coding. We distinguish between element-wise multiplication, which operates on corresponding elements of arrays, and matrix multiplication, which follows the rules of linear algebra.
Element-Wise Multiplication: The Dot Operator (.*)
Section titled “Element-Wise Multiplication: The Dot Operator (.*)”Use element-wise multiplication when you want to multiply the corresponding elements of two arrays. This is common for scaling data, applying a formula across a dataset, or working with non-linear equations.
Syntax
Section titled “Syntax”% The dot-asterisk operatorX = A .* B
% The equivalent function call (less common in scripts)X = times(A, B)For A .* B to work, arrays A and B must either have the exact same dimensions or have compatible dimensions for broadcasting (also known as implicit expansion).
Example 1: Multiplying Two Vectors
Section titled “Example 1: Multiplying Two Vectors”Let’s multiply two vectors of the same size.
% Define two row vectorsA = [1 2 3];B = [4 5 6];
% Perform element-wise multiplicationX = A .* BCommand Window Output:
X = 4 10 18Each element in X is the product of the elements in the same position in A and B (i.e., X(1)=A(1)*B(1), X(2)=A(2)*B(2), etc.).
Example 2: Broadcasting with a Scalar
Section titled “Example 2: Broadcasting with a Scalar”A common use case is scaling an entire matrix by a single number. This is a simple form of broadcasting.
% Define a 2x3 matrixM = [1, 2, 3; 4, 5, 6];
% Scale the entire matrix by 10Scaled_M = M .* 10Command Window Output:
Scaled_M = 10 20 30 40 50 60Example 3: Broadcasting a Row and Column Vector
Section titled “Example 3: Broadcasting a Row and Column Vector”Broadcasting allows MATLAB to implicitly expand arrays with singleton dimensions to match the size of another array. Here, we multiply a 1x3 row vector with a 4x1 column vector to produce a 4x3 matrix. This is extremely powerful for creating lookup tables or grids.
% Define a 1x3 row vectorrowVec = 1:3;
% Define a 4x1 column vector (note the transpose operator ')colVec = (1:4)';
% Multiply them. MATLAB expands rowVec down 4 times and colVec across 3 times.resultMatrix = colVec .* rowVecCommand Window Output:
resultMatrix = 1 2 3 2 4 6 3 6 9 4 8 12Matrix Multiplication: The Asterisk Operator (*)
Section titled “Matrix Multiplication: The Asterisk Operator (*)”Use matrix multiplication for linear algebra operations, such as solving systems of linear equations, performing geometric transformations, or working with state-space models. This is fundamentally different from the element-wise approach.
Syntax and Rules
Section titled “Syntax and Rules”% The asterisk operatorC = A * B
% The equivalent function callC = mtimes(A, B)For C = A * B, the inner dimensions must match. If A is an m x n matrix, B must be an n x p matrix. The resulting matrix C will be m x p.
Example 4: A Clear Comparison
Section titled “Example 4: A Clear Comparison”Let’s define two matrices and see the difference between the two types of multiplication.
% Define two 2x2 matricesA = [1, 2; 3, 4];B = [5, 6; 7, 8];
% Perform element-wise multiplicationC_elementwise = A .* B
% Perform matrix multiplicationC_matrix = A * BCommand Window Output:
C_elementwise = 5 12 21 32
C_matrix = 19 22 43 50Notice the completely different results! C_matrix(1,1) is calculated as (1*5 + 2*7) = 19, following the rules of matrix multiplication.
Best Practices and Common Errors
Section titled “Best Practices and Common Errors”- When to use which? Use
.*(and./,.^) for applying formulas to data. Use*(and/for matrix division) for linear algebra. - Common Error:
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix.This occurs when you use*but the inner dimensions don’t match. The fix is to either check your logic or use.*if that was your intent. - Performance: MATLAB’s matrix operations (
*) are highly optimized using underlying libraries like BLAS and LAPACK. For linear algebra, they are exceptionally fast.