Open In App

Python – Incremental K sized Row Matrix Initialization

Last Updated : 01 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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.

Python3




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.

Python3




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:

  • Set the value of K and num_rows to determine the size of the matrix to be created.
  • Generate a list of values to be used in the matrix using a list comprehension and the range function. The values are generated incrementally starting from 1 up to K * num_rows.
  • Create the matrix using another list comprehension. This creates a list of lists, with each inner list containing K elements from the matrix_values list. The range function is used to select the appropriate range of indices for each row.
  • Print the final matrix using the print statement.

Below is the implementation of the above approach:

Python3




# 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:

  • Set the row size K and the number of rows num_rows.
  • Create an empty list called matrix.
  • Use a for loop to iterate through each row of the matrix. The loop variable i goes from 0 to num_rows – 1.
  • Inside the first loop, create another empty list called row.
  • Use another for loop to iterate through each element of the current row. The loop variable j goes from 0 to K – 1.
  • Inside the second loop, append the value i*K + j + 1 to the row list.
  • After the second loop, append the row list to the matrix list.
  • After the first loop, the matrix list will contain all the rows of the matrix.
  • Print the matrix list.

Below is the implementation of the above approach:

Python3




# 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

Python3




# 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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads