Open In App

Python – Incremental K sized Row Matrix Initialization

Sometimes, while working with Python, we can have a problem in which we need to perform initialization of matrix with Incremental numbers. This kind of application can come in Data Science domain. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using loop + list slicing This task can be performed in brute way using loop. In this, we run a loop skipping by K (size of row required), to adjust numbers addition ahead.






# Python3 code to demonstrate
# Incremental K sized Row Matrix Initialization
# using loop + list slicing
 
# Initialization of row size
K = 3
 
# Incremental K sized Row Matrix Initialization
# using loop + list slicing
res = []
for idx in range(1, 10, K):
    sub = [idx, idx + 1, idx + 2]
    res.append(sub)
 
# printing result
print ("The Incremental Initialized Matrix is : " + str(res))

Output : 
The Incremental Initialized Matrix is : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(n), where n is the number of elements in the new res list 



Method #2: Using list comprehension This is just another way to perform this task. This performs in similar way as above, but in a shorter way. 




# Python3 code to demonstrate
# Incremental K sized Row Matrix Initialization
# using list comprehension
 
# Initialization of row size
K = 3
 
# Incremental K sized Row Matrix Initialization
# using list comprehension
res = [[i, i + 1, i + 2] for i in range(1, 10, K)]
 
# printing result
print ("The Incremental Initialized Matrix is : " + str(res))

Output : 
The Incremental Initialized Matrix is : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Time complexity: O(N), where N is the number of rows in the resulting matrix. The list comprehension iterates over the range (1, 10, K), which generates N values, where N is equal to (10 – 1) // K + 1.
Auxiliary space: O(NK), where N is the number of rows and K is the number of columns in the resulting matrix. 

Method #3 : Using Numpy Library
We can perform this task using numpy library. Numpy is a library for mathematical operations. It can be used to perform matrix operations.




import numpy as np
 
# Initialization of row size
K = 3
 
# Incremental K sized Row Matrix Initialization using Numpy
res = np.arange(1, 10).reshape(-1, K)
 
# printing result
print ("The Incremental Initialized Matrix is : \n", res)

Output:

The Incremental Initialized Matrix is : 
[[1 2 3]
[4 5 6]
[7 8 9]]

Time complexity: O(N*K)
Auxiliary Space: O(N*K) where N is the number of rows and K is the number of elements in each row.

Method 4: Using itertools

The itertools.count() function creates an iterator that starts from the first argument (1 in this case) and increments by the second argument (1 in this case) each time next() is called on it.

The list comprehension creates a 3xK matrix by iterating over the range of 3 (i.e. creating 3 rows) and then using another list comprehension to create each row with K elements. The next(iter) function call inside the inner list comprehension gets the next value from the iterator created using itertools.count(). Finally, the initialized matrix is printed.




import itertools
 
K = 3
 
# Using itertools to create an iterator that starts from 1 and increments by 1
iter = itertools.count(1, 1)
 
# Using list comprehension to create a matrix with K rows and 3 columns
res = [[next(iter) for j in range(K)] for i in range(3)]
 
# Printing the matrix
print("The Incremental Initialized Matrix is : " + str(res))

Output
The Incremental Initialized Matrix is : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Time complexity: O(N), where N is the number of elements in the matrix. 
Auxiliary space: O(N) because the entire matrix is stored in memory.

Method #5: Using list constructor and range function

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate
# Incremental K sized Row Matrix Initialization
# using list constructor and range function
 
# Initialization of row size
K = 3
num_rows = 3
 
# Using list constructor and range function
matrix_values = [i for i in range(1, K * num_rows + 1)]
matrix = [matrix_values[i:i+K] for i in range(0, K*num_rows, K)]
 
# printing result
print("The Incremental Initialized Matrix is : " + str(matrix))

Output
The Incremental Initialized Matrix is : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Time complexity: O(Knum_rows)
Auxiliary space: O(Knum_rows), since it creates a list of values for the entire matrix before converting it into a list of lists.

Method 6: Using nested for loops and append()

Step-by-Step Approach:

Below is the implementation of the above approach:




# Initialization of row size
K = 3
num_rows = 3
 
# Using nested for loops and append()
matrix = []
for i in range(num_rows):
    row = []
    for j in range(K):
        row.append(i*K + j + 1)
    matrix.append(row)
 
# printing result
print("The Incremental Initialized Matrix is : " + str(matrix))

Output
The Incremental Initialized Matrix is : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Time Complexity: O(Knum_rows)
Auxiliary Space: O(Knum_rows)

Method 7: Using map() and lambda function




# Initialization of row size
K = 3
num_rows = 3
 
# Using map() and lambda function
matrix = list(map(lambda i: [(i*K) + j + 1 for j in range(K)], range(num_rows)))
 
# printing result
print("The Incremental Initialized Matrix is : " + str(matrix))

Output
The Incremental Initialized Matrix is : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Time complexity: O(num_rows * K)
Auxiliary Space: O(num_rows * K)


Article Tags :