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.