Python – K Matrix Initialization
Last Updated :
17 Apr, 2023
Sometimes in the world of competitive programming, we need to initialise the matrix, but we don’t wish to do it in a longer way using a loop. We need a shorthand for this. This type of problem is quite common in dynamic programming domain. Let’s discuss certain ways in which this can be done.
Method #1: Using List comprehension List comprehension can be treated as a shorthand for performing this particular operation. In list comprehension, we can initialise the inner list with K and then extend this logic to each row again using the list comprehension.
Python3
N = 5
M = 4
K = 7
res = [ [ K for i in range (N) ] for j in range (M) ]
print ( "The matrix after initializing with K : " + str (res))
|
Output :
The matrix after initializing with K : [[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]
Time Complexity: O(n * m) where n is the number of rows and m is the number of columns
Auxiliary Space: O(n * m) where n is the number of rows and m is the number of columns
Method #2: Using list comprehension + “*” operator This problem can also be simplified using the * operator which can slightly reduce the tedious way task is done and can simply use multiply operator to extent the initialization to all N rows.
Python3
N = 5
M = 4
K = 7
res = [ [K for i in range (M)] * N]
print ( "The matrix after initializing with K : " + str (res))
|
Output :
The matrix after initializing with K : [[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]
Method #3: Using Numpy
Note: Install numpy module using command “pip install numpy”
One can also use the Numpy library to initialise the matrix. Numpy library provides a convenient way to initialise the matrix of given size and fill it with any given value.
Python3
import numpy as np
N = 5
M = 4
K = 7
res = np.full((N, M), K)
print ( "The matrix after initializing with K :\n" , res)
|
Output:
The matrix after initializing with K :
[[7 7 7 7]
[7 7 7 7]
[7 7 7 7]
[7 7 7 7]
[7 7 7 7]]
Time Complexity: O(n * m) where n is the number of rows and m is the number of columns
Auxiliary Space: O(n * m) where n is the number of rows and m is the number of columns
Method 4: using a nested for loop.
Step-by-step approach:
- Declare the number of rows and columns N and M.
- Initialize the value K.
- Create an empty list res to store the matrix.
- Use a nested for loop to iterate over the rows and columns and append K to the matrix.
- Print the matrix.
Below is the implementation of the above approach:
Python3
N = 5
M = 4
K = 7
res = []
for i in range (M):
row = []
for j in range (N):
row.append(K)
res.append(row)
print ( "The matrix after initializing with K : " + str (res))
|
Output
The matrix after initializing with K : [[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]
Time Complexity: O(NM)
Auxiliary Space: O(NM) to store the matrix.
Method #5: Using list multiplication
Step-by-step Approach:
- Declare the number of rows and columns in the matrix as N and M respectively.
- Initialize the value of K that will be used to initialize the matrix.
- Create an empty list res that will be used to store the matrix.
- Create a list of K values with length N using the list multiplication operator *. This will be used to initialize each row of the matrix.
- Use a list comprehension to create the matrix by repeating the row M times.
- Print the resulting matrix.
Python3
N = 5
M = 4
K = 7
row = [K] * N
res = [row for _ in range (M)]
print ( "The matrix after initializing with K : " + str (res))
|
Output
The matrix after initializing with K : [[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]]
Time Complexity: O(N*M), as each element in the matrix is initialized once.
Auxiliary Space: O(N*M), as the entire matrix is stored in memory.
Share your thoughts in the comments
Please Login to comment...