Matrix-scalar multiplication scales an entire matrix by a single number. This operation takes a scalar (a single number) and multiplies every element within the matrix by that scalar. The result is a new matrix where each original element has been scaled.
Imagine you have a grayscale image represented as a matrix, where each element is a pixel's brightness value. If you wanted to make the entire image twice as bright, you would simply multiply the value of every pixel by 2. This is a perfect analogy for matrix-scalar multiplication; you are uniformly scaling the entire grid of numbers.
If we have a matrix A and a scalar c, their product is written as cA. The result is a new matrix of the same dimensions as A, where each element is the product of the original element and the scalar.
Formally, if B=cA, then the element in the i-th row and j-th column of B is given by:
bij=c×aijThis operation is performed element by element, making it straightforward to compute. The diagram below illustrates this process with a concrete example.
The scalar
3multiplies each element of the matrix on its right, resulting in a new matrix where every value has been scaled by a factor of 3.
Let's walk through the calculation for the example above. Given the matrix A and the scalar c=3:
A=[125−3]The product 3A is calculated as:
3A=3×[125−3]=[3×13×23×53×−3]=[3615−9]Matrix-scalar multiplication appears frequently in machine learning algorithms, often in subtle but significant ways.
As you might expect, performing this operation in NumPy is simple and intuitive. You can use the standard multiplication operator * between a NumPy array (our matrix) and a scalar. NumPy automatically handles the element-wise multiplication for you.
Here's how you can perform the same calculation from our example in Python:
import numpy as np
# Define our matrix A
A = np.array([
[1, 5],
[2, -3]
])
# Define our scalar c
c = 3
# Perform scalar multiplication
B = c * A
print("Original Matrix A:")
print(A)
print("\nScalar c:", c)
print("\nResult c * A:")
print(B)
Output:
Original Matrix A:
[[ 1 5]
[ 2 -3]]
Scalar c: 3
Result c * A:
[[ 3 15]
[ 6 -9]]
The output from the NumPy code matches our manual calculation perfectly. NumPy's ability to perform these operations efficiently across entire arrays without needing explicit loops is a feature known as broadcasting, which is fundamental to writing clean and performant numerical code in Python.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with