0

I try to find a solution to found all the columns permutations of a matrix. So i wrote this code but it doesn't work.

SOLVED:

#! python
import numpy

def permutation(matrix): 

    if numpy.size(matrix,1) == 1:
        return [matrix]

    #empty list
    m=[]

    # Iterate the input(matrix) and calculate the permutation 
    for i in range(numpy.size(matrix,1)):
        column = matrix[:,[i]]

        # Extract column[i] or m from the matrix. remMatrix is the remaining matrix
        remMatrix = numpy.concatenate((matrix[:,:i], matrix[:,i+1:]), axis=1)
                   
        # Generating all permutations where m is the first element
        for p in permutation(remMatrix):
            m.append(numpy.concatenate([column,p],axis=1))
    return m


#driver to test the function
matrix=numpy.matrix('1 2 3; 0 0 0')
for p in permutation(matrix): 
    print(p)
Hike
  • 13
  • 3
  • Does this answer your question? [Permute rows and columns of a matrix](https://stackoverflow.com/questions/34438098/permute-rows-and-columns-of-a-matrix) – Carlo Zanocco Oct 02 '20 at 09:52

1 Answers1

0

You did not handle the base case in your recursion. If you pass a matrix with a single column, then permutation returns an empty matrix. That is because line m = numpy.concatenate((column,p),axis=1) will not be reached if remMatrix is empty.

As result m is an empty array once you return it and the print statement is not called.

I also don't fully understand what you're trying to do. Wouldn't there for most matrices be multiple column permutations? Do you want all those matrices concatenated in the end?

Martin
  • 199
  • 2
  • 6
  • Oh thanks! in fact I'm trying to extend the code for a list, found at the following link at method number 2, for the columns of a matrix, but I'm using python since a couple of days and I can't found the complete solution https://www.geeksforgeeks.org/generate-all-the-permutation-of-a-list-in-python/ – Hike Oct 02 '20 at 11:00
  • Ah I see. You actually removed the base cases `if len(lst) == 0` and `if len(lst) == 1` from the example but you need them also in your case ;) – Martin Oct 02 '20 at 11:08
  • I tried to add the two if statement and correct some mistakes, now i had some errors, but another thing is that I 'print(p)' I obtain an array with only one row, so when I try to concatenate or append it doesn't because the dimensions along axis 0 are different. I edit the code in the question with the current – Hike Oct 02 '20 at 12:11
  • `m` needs to be a list that you append to. This means that your base cases need to change a bit as well: `if numpy.size(matrix, 1) == 1: return [matrix]`. It seems you actually don't need `if numpy.size(matrix, 1) == 0` case. And the append becomes: `m.append(numpy.concatenate([column, p], axis=1))` – Martin Oct 02 '20 at 12:23
  • now it seems work! great, thanks so much! is it better to update the code in the question with the working one? – Hike Oct 02 '20 at 12:37
  • You're welcome. You could have a "Solved" section with your working solution in the end of your post. Also mark my answer as solved please :) – Martin Oct 02 '20 at 12:39