Home »
Python »
Python Programs
'Cloning' Row or Column Vectors to Matrix
In this tutorial, we will learn how to clone given row or column vectors to matrix using Python NumPy?
By Pranit Sharma Last updated : May 28, 2023
Problem Statement
Given row or column vectors, we have clone given vectors to a matrix using Python NumPy.
Creating clones of row or column vectors to a matrix
The easiest way for cloning row or column vectors to a matrix, you can use the numpy.tile() method which takes a vector or an array and returns the array repeated by a given number of repetitions.
Syntax:
numpy.tile(A, reps)
Let us understand with the help of examples,
Example 1: Cloning Row Vector to Matrix
# Import numpy
import numpy as np
# Row vector
arr = [1, 2, 3, 4]
# Printing "arr"
print("arr:\n", arr, "\n")
# Closing row vector to a matrix
res = np.tile(arr, (4, 1))
# Printing the result
print("Matrix:\n", res)
Output
arr:
[1, 2, 3, 4]
Matrix:
[[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]]
Example 2: Cloning Column Vector to Matrix
# Import numpy
import numpy as np
# Column vector
arr = [[4],
[5],
[6],
[7]]
# Printing "arr"
print("arr:\n", arr, "\n")
# Closing column vector to a matrix
res = np.tile(arr, (1, 4))
# Printing the result
print("Matrix:\n", res)
Output
arr:
[[4], [5], [6], [7]]
Matrix:
[[4 4 4 4]
[5 5 5 5]
[6 6 6 6]
[7 7 7 7]]
Python NumPy Programs »