Skip to content

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.

% The dot-asterisk operator
X = 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).

Let’s multiply two vectors of the same size.

% Define two row vectors
A = [1 2 3];
B = [4 5 6];
% Perform element-wise multiplication
X = A .* B

Command Window Output:

X =
4 10 18

Each 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.).

A common use case is scaling an entire matrix by a single number. This is a simple form of broadcasting.

% Define a 2x3 matrix
M = [1, 2, 3; 4, 5, 6];
% Scale the entire matrix by 10
Scaled_M = M .* 10

Command Window Output:

Scaled_M =
10 20 30
40 50 60

Example 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 vector
rowVec = 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 .* rowVec

Command Window Output:

resultMatrix =
1 2 3
2 4 6
3 6 9
4 8 12

Matrix 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.

% The asterisk operator
C = A * B
% The equivalent function call
C = 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.

Let’s define two matrices and see the difference between the two types of multiplication.

% Define two 2x2 matrices
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
% Perform element-wise multiplication
C_elementwise = A .* B
% Perform matrix multiplication
C_matrix = A * B

Command Window Output:

C_elementwise =
5 12
21 32
C_matrix =
19 22
43 50

Notice the completely different results! C_matrix(1,1) is calculated as (1*5 + 2*7) = 19, following the rules of matrix multiplication.

  • 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.