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 code to demonstrate # K Matrix Initialization # using list comprehension # Declaring rows N = 5
# Declaring columns M = 4
# initializing K K = 7
# using list comprehension # to initializing matrix res = [ [ K for i in range (N) ] for j in range (M) ]
# printing result print ( "The matrix after initializing with K : " + str (res))
|
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 code to demonstrate # K Matrix Initialization # using list comprehension # and * operator # Declaring rows N = 5
# Declaring columns M = 4
# initializing K K = 7
# using list comprehension # to initializing matrix res = [ [K for i in range (M)] * N]
# printing result print ( "The matrix after initializing with K : " + str (res))
|
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.
import numpy as np
# Declaring rows N = 5
# Declaring columns M = 4
# initializing K K = 7
# using numpy library # to initializing matrix res = np.full((N, M), K)
# printing result print ( "The matrix after initializing with K :\n" , res)
#This code is contributed by Edula Vinay Kumar Reddy |
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 code to demonstrate # K Matrix Initialization # using nested for loop # Declaring rows N = 5
# Declaring columns M = 4
# initializing K K = 7
# creating an empty matrix res = []
# using nested for loop for i in range (M):
row = []
for j in range (N):
row.append(K)
res.append(row)
# printing result print ( "The matrix after initializing with K : " + str (res))
|
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 code to demonstrate K Matrix # Initialization using list multiplication # Declaring rows N = 5
# Declaring columns M = 4
# initializing K K = 7
# creating a list of K values with length N row = [K] * N
# using list multiplication to create the matrix res = [row for _ in range (M)]
# printing result print ( "The matrix after initializing with K : " + str (res))
|
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.