Python | Matrix creation of n*n
Last Updated :
26 Feb, 2023
Many times while working with numbers in data science we come across the problem in which we need to work with data science we need to transform a number to a matrix of consecutive numbers and hence this problem has a good application. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using list comprehension List comprehension can be used to accomplish this particular task by using the range function for each list that needs to be constructed consecutively.
Python3
N = 4
print ( "The dimension : " + str (N))
res = [ list ( range ( 1 + N * i, 1 + N * (i + 1 )))
for i in range (N)]
print ( "The created matrix of N * N: " + str (res))
|
Output
The dimension : 4
The created matrix of N * N: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Time Complexity: O(n*n)
Auxiliary Space: O(n*n)
Method #2 : Using next() + itertools.count() The count function can be used start the count of numbers and next function does the task of creation of sublist consecutively. List comprehension handles the processing.
Python3
import itertools
N = 4
print ( "The dimension : " + str (N))
temp = itertools.count( 1 )
res = [[ next (temp) for i in range (N)] for i in range (N)]
print ( "The created matrix of N * N: " + str (res))
|
Output
The dimension : 4
The created matrix of N * N: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Method #3 Using a list comprehension and the enumerate() function:
In this code, the list comprehension iterates over the rows of the matrix (using the enumerate() function to get the index of each row), and generates the elements of the row using a nested list comprehension. The nested list comprehension iterates over the columns of the matrix and generates the elements of the column using the formula i * n + j, where i is the index of the row and j is the index of the column.
Python3
n = 4
matrix = [[i * n + j for j in range ( 1 , n + 1 )] for i, _ in enumerate ( range (n))]
print ( "The created matrix of N * N: " + str (matrix))
|
Output
The created matrix of N * N: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Time complexity: O(N^2)
Auxiliary Space: O(N^2)
Method #4 : Using for loop, while loop and slicing
Python3
N = 4
print ( "The dimension : " + str (N))
x = N * N
y = []
for i in range ( 1 ,x + 1 ):
y.append(i)
res = []
i = 0
while (i< len (y)):
a = y[i:i + N]
res.append(a)
i + = N
print ( "The created matrix of N * N: " + str (res))
|
Output
The dimension : 4
The created matrix of N * N: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Time Complexity : O(N*N)
Auxiliary Space : O(N)
Method #5 : Using nested for loop:
1.Initialize the value of N.
2.Create an empty list called ‘res’ to store the matrix.
3.Using nested for loops, create a matrix with dimensions N x N, and populate it with values from 1 to N x N, in row-major order.
a. For each row i, create an empty list called ‘row’.
b. For each column j in row i, append the value 1 + N * i + j to the list ‘row’.
c. Append the list ‘row’ to the list ‘res’.
4.Print the created matrix using the print() function.
Python3
N = 4
print ( "The dimension : " + str (N))
res = []
for i in range (N):
row = []
for j in range (N):
row.append( 1 + N * i + j)
res.append(row)
print ( "The created matrix of N * N: " + str (res))
|
Output
The dimension : 4
The created matrix of N * N: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Time Complexity:
The time complexity of the given code is O(N^2), since it iterates over N rows and N columns in the nested for loops.
Space Complexity:
The space complexity of the given code is O(N^2), since it stores N^2 elements in the ‘res’ list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...