# nxnxn matrix python

In this tutorial, we will take a look at how to create the nxnxn matrix in Python.

## What is NxNxN?

The term NxNxN (pronounced as N by N by N) is also called as NxNxN cube or NxNxN puzzle. It represents the cube with the same dimensions that means the cube will have the same height, width, and length.

The NxNxN puzzles that fit under this category include the 2x2x2 cube, the Rubik’s cube, the 4x4x4 cube, the 5x5x5 cube, etc. The 1x1x1 cube also belongs in this category, even if it is not a twisty puzzle because it does complete the NxNxN set.

## How to Create NxNxN Matrix in Python?

Now that we know what is nxnxn, lets us learn how to create the nxnxn matrix in Python using different ways with examples.

## Create NxN Matrix in Python 3 with Non Duplicating numbers

The below code is to create an nxn matrix in Python, and it does not repeat the numbers row-wise and column-wise. These are mainly used in Puzzles like Sudoko.

``````# Python Program to create nxn Matrix
import numpy as np

# Provide the value of N
N = 5

# returns evenly spaced values
row = np.arange(N)

# create an new array filled with zeros of given shape and type
result = np.zeros((N, N))

# Logic to roll array elements of given axis
for i in row:
result[i] = np.roll(row, i)

print(result)``````

Output

``````[[0. 1. 2. 3. 4.]
[4. 0. 1. 2. 3.]
[3. 4. 0. 1. 2.]
[2. 3. 4. 0. 1.]
[1. 2. 3. 4. 0.]]``````

## Create NxNxN matrix in Python using numpy

The below code is to create an nxnxn matrix in Python 3. Just change the value of N based on the requirement and the shape that you need to generate. For a standard Rubik’s cube, it would be 3x3x3, so the value of n would be 3.

Example:

``````# Program to nxnxn matrix python 3
import numpy as np

# Provide the value of nxnxn
n = 3
a = np.arange(n)
b = np.array([a]*n)
matrix = np.array([b]*n)

#creating an array containg n-dimensional points
flat_mat = matrix.reshape((int(matrix.size/n),n))

#just a random matrix we will use as a rotation
rotate = np.eye(n) + 2

#apply the rotation on each n-dimensional point
result = np.array([rotate.dot(x) for x in flat_mat])
result=result.reshape((n,n,n))
print(result)``````

Output

``````[[[6. 7. 8.]
[6. 7. 8.]
[6. 7. 8.]]

[[6. 7. 8.]
[6. 7. 8.]
[6. 7. 8.]]

[[6. 7. 8.]
[6. 7. 8.]
[6. 7. 8.]]]``````

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

##### You May Also Like ## Python urllib.error.httperror: http error 403: forbidden

The urllib.error.httperror: http error 403: forbidden occurs when you try to scrap a webpage using urllib.request module and the mod_security blocks the request. There are several reasons why you get this error.… ## Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

Table of Contents Hide How to solve Python was not found; run without arguments to install from the Microsoft StoreScenario 1: Python is not installedScenario 2: Environment variables are not… ## ModuleNotFoundError: No module named ‘OpenSSL’

Table of Contents Hide What is ModuleNotFoundError: No module named ‘openssl’?How to fix ModuleNotFoundError: No module named ‘openssl’?Solution 1 – Installing and using the pyOpenSSL module in a proper waySolution… ## [Solved] AttributeError: ‘int’ object has no attribute ‘get’

Table of Contents Hide What is AttributeError: ‘int’ object has no attribute ‘get’?How to fix AttributeError: ‘int’ object has no attribute ‘get’?Solution 1 – Call the get() method on valid… ## Python complex()

Table of Contents Hide complex() Syntax complex() Parameterscomplex() Return ValueExample 1: How to create a complex number in Python?Example 2: TypeError while converting string with imaginary parameterExample 3: ValueError if string… ## How to Create a Pie Chart in Seaborn

We do not have any built-in function to create Pie chart in seaborn, but with the help of Matplotlib, we can create a pie chart and leverage seaborn for color…