matrix operations | repmat () function

NumPy | Python Methods and Functions

numpy.matlib.repmat() — this is another function to do matrix operations in numpy. Returns a Repeat 0-D, 1-D, or 2-D array, or an M x N matrix.

Syntax: numpy.matlib.repmat (a, m, n)

Parameters:
a: [array_like] The input array or matrix which to be repeated.
m, n: [int] The number of times a is repeated along the first and second axes.

Return: [ndarray] repeating array.

Code # 1:

# Python program explaining
# numpy.matlib.repmat () function

 
# import numpy library and matrix

import numpy as geek

import numpy.matlib

 
# create an input array using
# array function

in_arr = geek.array ([[ 1 , 0 , 2 ], [ 3 , 4 , 5 ]])

print ( "Input array" , in_arr) 

 
# creating a new array
# using the repmat () function

out_mat = geek.matlib.repmat (in_arr, 2 , 3

print ( " Output repeated matrix: " , out_mat) 

Output:

 Input array [[ 1 0 2] [3 4 5]] Output repeated matrix: [[1 0 2 1 0 2 1 0 2] [3 4 5 3 4 5 3 4 5] [1 0 2 1 0 2 1 0 2] [3 4 5 3 4 5 3 4 5]] 

Code # 2:

# Python program explaining
# numpy.matlib.repmat () function

 
# import numpy library and matrix

import numpy as geek

import numpy.matlib

 
# create an input array using
# orange function

in_arr = geek.arange ( 3 )

print ( "Input array" , in_arr) 

 
# create a new array
# using the repmat () function

out_mat = geek.matlib.repmat (in_arr, 2 ,   2

print ( "Output repeated matrix:" , out_mat) 

Output:

 Input array [0 1 2] Output repeated matrix: [[0 1 2 0 1 2] [0 1 2 0 1 2]] 




Get Solution for free from DataCamp guru