Results 1 to 4 of 4
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    379
    Rep Power
    6

    Default Minor of a matrix

    I have a 4x4 matrix that I need to find the minor of.

    4 5 6 2
    3 4 5 5
    7 4 3 3
    5 7 6 8

    The minor is basically the resultant matrix that you get when you remove the row/column that [i,j] is in. So for instance, if I had a Matrix(A), and I needed the minor of A[2,3], I would be removing (from the above) I would end up with the following matrix:

    4 5 6
    3 4 5
    5 7 6


    Any ideas on how I could get that result? Preferably something where I could pass in the 2d array representing the matrix, and the i,j position that I wanted to eliminate.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Why do you need to find the minor of a matrix? Do you want to find the determinant of a matrix? There are better ways ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    379
    Rep Power
    6

    Default

    yes, the assignment is to recursively find the determinant of a matrix, preferably using the minor. so its something like

    public double FindDeterminant(double[][] inputMatrix){
    double determinant = 0;
    //logic

    return determinant;
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by sehudson View Post
    yes, the assignment is to recursively find the determinant of a matrix, preferably using the minor. so its something like

    public double FindDeterminant(double[][] inputMatrix){
    double determinant = 0;
    //logic

    return determinant;
    }
    If you really have to use the minors of a matrix I'd use 'views' on the matrix; here's how: first define an interface:

    Java Code:
    interface MatrixView {
    	double get(int i, int j);
    	int dim();
    }
    This doesn't say much but here's a RawMatrixView that offers a 1 to 1 view of a raw matrix:

    Java Code:
    class RawMatrixView implements MatrixView {
    	private double[][] mat;
    	
    	public RawMatrixView(double[][] mat) {
    		this.mat= mat;
    	}
    	
    	public double get(int i, int j) {
    		return mat[i][j];
    	}
    	
    	public int dim() {
    		return mat.length;
    	}
    }
    This doesn't say much either but given this first class we can build a MinorMatrixView:

    Java Code:
    class MinorMatrixView implements MatrixView {
    	MatrixView mv;
    	int r, c;
    	
    	MinorMatrixView(MatrixView mv, int r, int c) {
    		this.mv= mv;
    		this.r= r;
    		this.c= c;
    	}
    	
    	public double get(int i, int j) {
    		if (i >= r) i++;
    		if (j >= c) j++;
    		return mv.get(i, j);	
    	}
    	
    	public int dim() {
    		return mv.dim()-1;
    	}
    }
    As you can see from this class if a row or column element is refered it simply returns the element in the next row or column. And, more important all MatrixView objects can be wrapped by others allowing for a recursive approach (needed by your determinant finding method).

    Here's a small class that tests the entire thing:

    Java Code:
    public class T {
    	public static void main(String[] args) {
    		double[][] mat= { 
    				{ 1, 2, 3, 4 },
    			    { 2, 3, 4, 5 },
    			    { 3, 4, 5, 6 },
    			    { 4, 5, 6, 7 }
    		};
    		
    		MatrixView rmv= new RawMatrixView(mat);
    		MinorMatrixView mmv= new MinorMatrixView(rmv, 1, 2);
    		
    		for (int i= 0; i < mmv.dim(); i++) {
    			for (int j= 0; j < mmv.dim(); j++)
    				System.out.print(mmv.get(i, j)+" ");
    			System.out.println();
    		}
    	}
    }
    This class builds a minor (ignore row 1, column 2) of a raw matrix. I think you can take it from here.

    kind regards,

    Jos
    Last edited by JosAH; 02-22-2011 at 12:20 PM. Reason: fixed a bit of code in the MinorMatrixView class; sorry about the mistake ...
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Help with dox matrix printer
    By Albert in forum Advanced Java
    Replies: 7
    Last Post: 09-06-2011, 08:50 AM
  2. reading Matrix
    By metrokid in forum New To Java
    Replies: 1
    Last Post: 12-04-2010, 01:48 PM
  3. Matrix class
    By maz22 in forum New To Java
    Replies: 5
    Last Post: 10-15-2010, 06:53 AM
  4. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 01:26 PM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •