Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Matrix creation of n*n

Python | Matrix creation of n*n

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




# Python3 code to demonstrate
# matrix creation of n * n
# using list comprehension
 
# initializing N
N = 4
 
# printing dimension
print("The dimension : " + str(N))
 
# using list comprehension
# matrix creation of n * n
res = [list(range(1 + N * i, 1 + N * (i + 1)))
                            for i in range(N)]
 
# print result
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




# Python3 code to demonstrate
# matrix creation of n * n
# using next() + itertools.count()
import itertools
 
# initializing N
N = 4
 
# printing dimension
print("The dimension : " + str(N))
 
# using next() + itertools.count()
# matrix creation of n * n
temp = itertools.count(1)
res = [[next(temp) for i in range(N)] for i in range(N)]
 
# print result
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




# Initialize the size of the matrix
n = 4
 
# Create the matrix using a list comprehension and the enumerate() function
matrix = [[i * n + j for j in range(1, n+1)] for i, _ in enumerate(range(n))]
 
# Print the matrix
print("The created matrix of N * N: "+ str(matrix))
#This code is contributed by Edula Vinay Kumar Reddy


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




# Python3 code to demonstrate
# matrix creation of n * n
 
# initializing N
N = 4
 
# printing dimension
print("The dimension : " + str(N))
 
# using list comprehension
# matrix creation of n * 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 result
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




# Python3 code to demonstrate
# matrix creation of n * n
# using nested for loops
 
# initializing N
N = 4
 
# printing dimension
print("The dimension : " + str(N))
 
# initializing empty matrix
res = []
for i in range(N):
  row = []
  for j in range(N):
    row.append(1 + N * i + j)
  res.append(row)
 
# print result
print("The created matrix of N * N: " + str(res))
#This code is contributed by Jyothi pinjala.


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.

RELATED ARTICLES

Most Popular

Recent Comments