# Matrix of Minors

This topic is 2655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I have been trying to figure this out for a while and I can't do it! I just need a function that will take a column major square matrix (2x2, 3x3, or 4x4) and also the rowIndex and colIndex of the dimensions that are to be cancelled. For example the method signature would be like MatrixMinor(int rowIndex, int colIndex, double[] matrix)

So if I sent a 4x4 (Length = 16) matrix with a rowIndex of 0 and colIndex of 0 it should return a 3x3 matrix with the rowIndex = 0 and colIndex = 0 elements removed. So here is the initial matrix, these are the array indices (column major):

0 4 8 12
1 5 9 13
2 6 10 14
3 7 11 15

if i send this matrix to the function with rowIndex = 0 and colIndex = 0 it should return this matrix as a column major array:

5 9 13
6 10 14
7 11 15

I'm trying to do a Matrix of Minors calculation on a square matrix. Basically I can't come up with the math to determine what the array indexes should be according to the rowIndex and colIndex sent to the method. I have the code for getting the determinent but I can't figure out the code for just getting a minor matrix from an input matrix.

Thanks!

##### Share on other sites
Not completely sure but you can try doing something like this :
typedef std::vector<double> MatrixType;MatrixType doIt(const MatrixType& mat,const int matSize,int rowToRemove, int colToRemove){// validate(mat,matSize,rotToRemove,colToRemove); MatrixType minorMat; for(int row = 0; row != matSize; ++row){      if(row == rowToRemove) continue;   for(int col = 0; col != matSize; ++col){       if(col != colToRemove){           minorMat.push_back( mat[ matSize*row + col] );       }   } }  return minorMat;}

The above code is not compiled nor do I guarantee that it works. Its just to present you an idea.

##### Share on other sites
Great that works, thanks so much! :)

##### Share on other sites
Let me ask: Are you using the adjugate matrix to compute an inverse matrix? FYI, that's a really slow way to get the inverse. Gaussian elimination / LU decomposition is a better general-purpose method. And for the specific matrices you're likely to encounter in games, you can do even better; 4x4 homogeneous matrices are easily inverted by doing little more than transposing the rotation part, and mass matrices (in physics) are positive definite and so can be inverted more efficiently using Cholesky decomposition.

EDIT: I just noticed your matrices are all small. For very small matrices, Cramer's rule is actually efficient because you can parallelize it well and use SIMD. But still, you can do better if you've got homogeneous transforms.