Open In App

Python – Incremental Range Initialization in Matrix

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python, we can have a problem in which we need to perform the initialization of Matrix. Simpler initialization is easier. But sometimes, we need to perform range incremental initialization. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using loop This is brute way in which this task can be performed. In this, we iterate through the list and increase the value of element with the range specified. 

Python3




# Python3 code to demonstrate
# Incremental Range Initialization in Matrix
# using loop
 
# Initializing row
r = 4
 
# Initializing col
c = 3
 
# Initializing range
rang = 5
 
# Incremental Range Initialization in Matrix
# using loop
res = []
temp = []
temp2 = 0
for idx in range(r):
    for idx in range(c):
        temp.append(temp2)
        temp2 = temp2 + rang
    res.append(temp)
    temp = []
 
# printing result
print ("Matrix after Initialization : " + str(res))


Output : 

Matrix after Initialization : [[0, 5, 10], [15, 20, 25], [30, 35, 40], [45, 50, 55]]

Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements

Method #2: Using list comprehension This task can also be performed using list comprehension. The method is similar to above. The difference is this is a shorthand to perform this task. 

Python3




# Python3 code to demonstrate
# Incremental Range Initialization in Matrix
# using list comprehension
 
# Initializing row
r = 4
 
# Initializing col
c = 3
 
# Initializing range
rang = 5
 
# Incremental Range Initialization in Matrix
# using list comprehension
res = [[rang * c * y + rang * x for x in range(c)] for y in range(r)]
 
# printing result
print ("Matrix after Initialization : " + str(res))


Output : 

Matrix after Initialization : [[0, 5, 10], [15, 20, 25], [30, 35, 40], [45, 50, 55]]

Method #3: Using numpy

  • Import the numpy library: import numpy as np
  • Create the incremental range matrix using NumPy:nres = np.arange(0, r*c*rang, rang).reshape(r,c)

Python3




import numpy as np
 
# Initializing row
r = 4
 
# Initializing col
c = 3
 
# Initializing range
rang = 5
 
# Incremental Range Initialization in Matrix using numpy
res = np.arange(0, r*c*rang, rang).reshape(r,c)
 
# printing result
print("Matrix after Initialization : " + str(res))


Output: 

Matrix after Initialization : [[ 0  5 10]
[15 20 25]
[30 35 40]
[45 50 55]

Time complexity: O(r * c) because the reshape() function takes constant time and the arange() function has a time complexity of O(n), where n is the number of elements in the range. Here, n = r * c * rang.
Auxiliary space: O(r * c) because the reshape() function creates a new matrix of size r * c, which requires O(r * c) space. 

Method #4: Using  map and lambda function:

1.Initialize the number of rows r, number of columns c, and the range rang.
2.Use a lambda function with map to create the matrix:
   a. The lambda function takes one argument i, representing the row index.
   b. The lambda function returns a list of values for that row using the formula rang * j + rang * c * i for each column index j.
   c. map applies the lambda function to each row index in the range range(r), resulting in a list of lists which is the desired matrix.
3.Print the matrix in the desired format using a for loop to iterate over the rows and print them one by one.

Python3




# Initializing row
r = 4
 
# Initializing col
c = 3
 
# Initializing range
rang = 5
 
# Using lambda function to create matrix
matrix = list(map(lambda i: [rang * j + rang * c * i for j in range(c)], range(r)))
 
# printing result
print("Matrix after Initialization :")
for row in matrix:
    print(row)
#This code is contributed by Jyothi pinjala.


Output

Matrix after Initialization :
[0, 5, 10]
[15, 20, 25]
[30, 35, 40]
[45, 50, 55]

Time complexity:

The time complexity of this algorithm is O(r * c) since we need to iterate over each row and column once to create the matrix.

Auxiliary Space:

The space complexity of this algorithm is O(r * c) since we need to store the entire matrix in memory.



Last Updated : 15 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads