How to create a row vector and a column vector using Python NumPy?
We can use the following Python code to create a row vector and a column vector using NumPy.
import numpy
r1 = numpy.array([[1, 2, 3]])
c1 = numpy.array([[1], [2], [3]])
print("Row vector r1: \n", r1)
print("Column vector c1: \n", c1)
Here, we are using the numpy.array() function to create a row vector and a column vector. The row vector has three numbers in one row. And the column vector has three numbers in a single column.
The output of the mentioned program will be:
Row vector r1: [[1 2 3]] Column vector c1: [[1] [2] [3]]
Please note that we can also create a row vector or a column vector containing n random numbers. We can use the following Python code for that purpose:
import numpy
n = 3
r2 = numpy.random.randint(low=1, high=10, size=(1, n))
c2 = numpy.random.randint(low=1, high=10, size=(n, 1))
print("Row vector r2: \n", r2)
print("Column vector c2: \n", c2)
Here, we are using the numpy.random.randint() function to generate random numbers. The generated random numbers will be within the half-open interval [low, high). And the shape of the generated array is given by the size argument. So, when size=(1, n), the created vector will be a row vector containing n numbers in a single row. And when size=(n, 1), the generated vector will be a column vector containing n numbers in a single column.
The output of the mentioned program will be:
Row vector r2: [[1 7 4]] Column vector c2: [[9] [8] [1]]








































0 Comments