How to perform matrix scalar multiplication?
Let’s say A is a matrix with the following elements.
And λ = 3 is a scalar. If we multiply A and λ, we will get another matrix B such that:
In other words, if we multiply a matrix with a scalar, then all the elements of the matrix gets multiplied by the scalar.
How to perform matrix scalar multiplication using Python NumPy?
We can use the following Python code to perform a matrix-scalar multiplication using NumPy.
import numpy m = 3 n = 4 A = numpy.random.randint(low=1, high=10, size=(m, n)) l = 3 B = l * A print("A: \n", A) print("l: ", l) print("B = l x A: \n", B)
Here, we are first creating an mxn matrix with random integers. We are using the numpy.random.randint() function for that purpose. The generated integers will be within the range [low, high).
The size argument specifies the shape of the matrix. For example, size=(m, n) specifies that the created matrix will have m rows and n columns.
We are them multiplying the generated matrix with a scalar l. The output of the mentioned program will be:
A: [[3 6 1 1] [9 2 3 1] [8 1 2 5]] l: 3 B = l x A: [[ 9 18 3 3] [27 6 9 3] [24 3 6 15]]






0 Comments