We can use the following Python code to create an mxn matrix that contains random integers.
import numpy m = 3 n = 4 A = numpy.random.randint(low=1, high=10, size=(m, n)) print("A: \n", A)
Here, we are creating a 3×4 matrix A using Python NumPy. The matrix A contains random integers. We are using numpy.random.randint() to generate random integers. The generated random integers will be within the range [low, high).
And the size argument specifies the size of the matrix. For example, size=(m, n) specifies that the created matrix will have m rows and n columns.
The output of the mentioned program will be:
A: [[2 1 1 2] [4 1 3 6] [1 3 3 9]]
Please note that we can also use a Python list of list to create a matrix.
import numpy l = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] B = numpy.array(l) print("B: \n", B)
Here, l is a list of list. We are using the numpy.array() function to convert the list into a matrix. The output of the above program will be:
B: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]






0 Comments