Pablo Estrada
Pablo Estrada

Reputation: 3452

Numpy: Get all row and column combinations of a given matrix

Is there any easy way I can get with numPy (or any other python library) the combination of rows an columns of a given matrix?

For example if I give this matrix:

A = np.array(
[1,2,3],
[4,5,6],
[7,8,9]
)

I would get an array like this ( With all possible equivalent matrixes )

A = np.array(
[1,2,3],
[4,5,6],
[7,8,9]
)
RESULT =[ 
[
[1,2,3],
[4,5,6],
[7,8,9]
],
[
[4,5,6],
[1,2,3],
[7,8,9]
],
[
[4,5,6],
[7,8,9],
[1,2,3]
],
[
[7,8,9],
[4,5,6],
[1,2,3]
],
[
[8,7,9],
[4,5,6],
[1,2,3]
],
..
..
etc
]

Upvotes: 0

Views: 1324

Answers (1)

Longwen Ou
Longwen Ou

Reputation: 879

You can do it with itertools:

import itertools

for item in itertools.permutations(A.reshape(9), 9):
    print(np.array(item).reshape(3,3))

Upvotes: 3

Related Questions