Subtracting a vector from a matrix involves a concept known as "broadcasting," where the vector's values are applied across the compatible dimensions of the matrix. The method depends on whether the vector is a column vector or a row vector.
Understanding Vector-Matrix Subtraction
When subtracting a vector from a matrix, the operation doesn't happen element-by-element unless the vector has the same dimensions as the matrix (in which case it's just element-wise subtraction). Instead, the vector's values are broadcast across the matrix dimensions that match its shape, allowing the subtraction to occur.
There are primarily two common scenarios based on the vector's orientation:
1. Subtracting a Column Vector
If you have a column vector, its elements are subtracted from the corresponding elements in each row of the matrix.
- Reference 1 states: "If you subtract a column vector, each row of the vector is subtracted from each row of the matrix."
- This means if your column vector has 'm' rows (and 1 column), and your matrix has 'm' rows and 'n' columns, the i-th element of the column vector is subtracted from every element in the i-th row of the matrix.
Let A be an m x n matrix and v be an m x 1 column vector.
The subtraction A - v results in a new m x n matrix where each element (A - v)ij
is calculated as Aij - vi
.
Example:
Consider matrix A (3x2) and column vector v (3x1):
A = | 10 20 |
| 30 40 |
| 50 60 |
v = | 5 |
| 6 |
| 7 |
Following the rule (subtracting vi
from the i-th row of A):
Row 1 of A: [10, 20]
- v1
(5
) = [10-5, 20-5]
= [5, 15]
Row 2 of A: [30, 40]
- v2
(6
) = [30-6, 40-6]
= [24, 34]
Row 3 of A: [50, 60]
- v3
(7
) = [50-7, 60-7]
= [43, 53]
Resulting Matrix A - v:
| 5 15 |
| 24 34 |
| 43 53 |
2. Subtracting a Row Vector
If you have a row vector, its elements are subtracted from the corresponding elements in each column of the matrix.
- Reference 2 states: "If you subtract a row vector, each column of the vector is subtracted from each column of the matrix."
- This means if your row vector has 1 row and 'n' columns, and your matrix has 'm' rows and 'n' columns, the j-th element of the row vector is subtracted from every element in the j-th column of the matrix.
Let A be an m x n matrix and u be a 1 x n row vector.
The subtraction A - u results in a new m x n matrix where each element (A - u)ij
is calculated as Aij - uj
.
Example:
Consider matrix A (3x2) and row vector u (1x2):
A = | 10 20 |
| 30 40 |
| 50 60 |
u = | 5 10 |
Following the rule (subtracting uj
from the j-th column of A):
Column 1 of A: [10, 30, 50]
- u1
(5
) = [10-5, 30-5, 50-5]
= [5, 25, 45]
(as a column vector)
Column 2 of A: [20, 40, 60]
- u2
(10
) = [20-10, 40-10, 60-10]
= [10, 30, 50]
(as a column vector)
Resulting Matrix A - u:
| 5 10 |
| 25 30 |
| 45 50 |
Compatibility
For these subtraction operations to be valid using broadcasting, the dimensions must be compatible:
- To subtract a column vector v (m x 1) from a matrix A (m x n), the number of rows must match.
- To subtract a row vector u (1 x n) from a matrix A (m x n), the number of columns must match.
In summary, subtracting a vector from a matrix relies on broadcasting the vector across the matrix dimension that corresponds to the vector's non-singleton dimension (rows for a column vector, columns for a row vector).