javamatrixjama

Jama matrix dimensions must agree error even when dimensions are matching


I'm doing some matrix operations in image processing using JAMA. Here, I'm multiplying a matrix with its transpose. For sure it is possible. But I'm receiving the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Matrix dimensions must agree.
    at Jama.Matrix.checkMatrixDimensions(Matrix.java:1041)
    at Jama.Matrix.arrayTimes(Matrix.java:615)
    at javaapplication52.JavaApplication52.main(JavaApplication52.java:66)

My code snippet is:

double dd[][]={{0,1,2,3,4,5,6,7,8},{1,2,3,4,5,6,7,8,9},{2,3,4,5,6,7,8,9,10}};
       Matrix M=new Matrix(dd);
       Matrix MT=M.transpose();
       Matrix C=MT.arrayTimes(M);
       double CC[][]=C.getArray();

     for(int i=0;i<3;i++)
{
    for(int j=0;j<3 ;j++)
    { System.out.println(CC[i][j]+" ");
    }
    System.out.println("\n ");
    //nextLine();
}

Solution

  • For matrix multiplication, you should use MT.times(M).

    MT.arrayTimes(M) is used for element multiplication.