Find minor matrix in python

Viewed 5754
def getMatrixMinor(m,i,j):
    return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])]

The above is the code that I find on the stack overflow in order to find the inverse of a matrix, however, I am really new in python. Can anybody explain the behind mechanism in order to construct a matrix of minor by using this code?

By the way, I've used this code and it works but I just don't understand how it works.

4 Answers

Let's start with the definition of (i,j)th minor matrix of a matrix:

(i,j)th minor of a matrix of size n is a smaller matrix of size n-1 with the i'th row and j'th column deleted.

Now lets look at this python one liner:

[row for row in (m[:i] + m[i+1:])]

m[:i] gives the first i rows of m (remember that we are representing a matrix as a list of rows, and adding two lists gives back a bigger list in python), now let's look at another one liner:

row[:j] + row[j+1:]

This gives us all the elements of a row except the j'th element (lst[a:b] gives a list with elements from lst between a and b, with b excluded)

Combine the above two and you get an expression which returns a new matrix with i'th row and j'th column excluded:

def getMatrixMinor(m,i,j):
    return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])]

Which is the minor matrix.

The function is very simple, used to compute the first minor matrices. Suppose you have a squared matrix of size m x m, and you want to compute the first minor matrix at position (i,j), what do you do? You have to extract a smaller matrix of size (m-1) x (m-1) from the original matrix by removing the row i-th and the column j-th, which is done by the function.

Concretely, m[:i]+m[i+1:] removes the i-th row and creates a new list of the remaining rows. For each row in this list, the function remove the element row[j]. The output of the function is a list of lists (rows) representing the corresponding minor matrix.

I recommend you to have a look at Python list comprehension and list slicing.

Maybe it will be useful for somebody, just want to share with you my working solution, any constructive critic is acceptable

def getMatrixMinor(arr,i,j):
    c = arr[:]
    c = np.delete(c, (i),axis=0)
    return [np.delete(row, (j),axis=0) for row in (c)]

As practical example you can see my implementation here based on this function for determinant calculation.

# Finding minors of a matrix and it's determinant

import numpy as np
from numpy import *

n = 4
arr = random.randint(0,10,(n,n))
print(arr)

def getMatrixMinor(arr,i,j):
    c = arr[:]
    c_r = np.delete(c, (i),axis=0) # deletes i-th row
    c_c = np.delete(c_r, (j),axis=1) #deletes j-th column
    return c_c

gmm = getMatrixMinor(arr,2,2)
print("gmm = \n",gmm)

d = linalg.det(gmm)
print("d =",d)

    [[6 3 4 6]
     [0 8 5 2]
     [2 9 6 3]
     [6 5 2 1]]
    gmm = 
     [[6 3 6]
     [0 8 2]
     [6 5 1]]
     d = -263.99999999999994,
     
     Thanks GoAI, mine is a slight change from your answer.
Related