The matrix in mathematics is a rectangular array arranged in rows and columns. In Python, a 3-dimensional matrix is denoted as “nxnxn”. The “nxnxn” matrix represents the matrix’s height, width, and depth. Different methods are utilized in Python to calculate the “nxnxn” matrix.
This blog post will explain various methods, along with appropriate examples in detail, to create the “nxnxn” matrix.
- Using Numpy Module to Create nxnxn Matrix
- Using List Comprehension to Create nxnxn Matrix
- Using For Loop to Create nxnxn Matrix
Method 1: Using Numpy Module to Create nxnxn Matrix
The Numpy module provides various functions to deal with an nxnxn matrix in Python, such as “np.zeros()”, “np.arange()”, etc. Let’s understand it via the examples given below:
Example 1: Create nxnxn of Zeros Matrix
In the below example, the “numpy.zeros()” function is used to create the “nxnxn” matrix in Python:
Code:
import numpy
num = 3
matrix = numpy.zeros((num,num,num))
print(matrix)
In the above code:
- The “numpy.zero()” function creates the “nxnxn” matrix filled with “zeros”.
- The nxnxn “matrix” is printed on the screen.
Output:
The above output shows a three-dimensional matrix containing zeros.
Example 2: Create nxnxn of Non-Zero Numbers Matrix
To create the “nxnxn” 3-dimensional matrix of non-zero numbers, use the NumPy module.
Code:
import numpy
num = 3
matrix = numpy.arange(num)
output = numpy.zeros((num,num,num))
for item in matrix:
output[item] = numpy.roll(matrix, item)
print(output)
In the above code:
- Import the “numpy” module and assign the size of the matrix value to a variable “num”.
- The “numpy.arange()” function is used to create the 1-D matrix array that contains the value of the matrix such as [0, 1, 2].
- The “numpy.zeros()” function is used to create a 3-D matrix that contains zeros.
- The “for loop” is utilized to iterate over the items of a given matrix.
- The “numpy.roll()” function accepts the matrix and item as an argument and a rolled version of a matrix with an offset equal to the item’s current value.
- The “output” slice along the first dimension will be allocated the roll function result.
Output:
The above output shows a 3-dimensional matrix of “3 x 3 x 3”.
Method 2: Using List Comprehension to Create an nxnxn Matrix
The list comprehension method is also used to create the “nxnxn” matrix in Python. Here is an example:
Code:
num = 3
matrix = [[[0 for k in range(num)] for j in range(num)] for i in range(num)]
print(matrix)
In the above code:
- The size of the matrix is assigned to a variable “num”.
- The nested for loops are used to iterate over the range function and create the “nxnxn” matrix.
Output:
The above snippet displays the “nxnxn” matrix containing the zeros as elements.
Method 3: Using For Loop to Create nxnxn Matrix
The traditional “for” loop is also used to create the “nxnxn” matrix in Python. The for loop is used along with the list comprehension method to create the “nxnxn” matrix:
Code:
num = 3
matrix = [item for item in range(num)]
result = [[[0 for k in range(num)] for j in range(num)] for i in range(num)]
for item in range(num):
for j in range(num):
for k in range(num):
result[item][j][k] = matrix[(j + item) % num]
print(result)
In the above code:
- The size of the matrix is assigned to the variable “num”.
- The “List Comprehension” method is used along with a for loop to create a 1-D array matrix such as [0, 1, 2].
- The nested list comprehension creates the “nxnxn” 3-D matrix filled with zeros.
- Now, the “for loop” assigns the result matrix with the values from the matrix array.
- The multiples nested for loop iterate over the 1st, 2nd, and 3rd dimension of the output matrix.
- The “result [ i ][ j ][ k ]” is assigned the value of the “matrix [j + item] % num” that creates the “nxnxn” matrix.
Output:
The above snippet displays the “nxnxn” matrix containing the non-zero values.
Conclusion
To create the “nxnxn” matrix in Python, the NumPy module, List comprehension, and “for loop” along with list comprehension are used. The “np.zeros(n,n,n)” function is used to create the “nxnxn” matrix filled with zeros. The “np.zeros()” is also used along with the “np.roll()” function to create an “nxnxn” matrix of non-zero numbers. This guide provided a detailed guide on creating the “nxnxn” matrix using various examples.