0

Hi I have this code for multiplication of matrices in Java. It is working (no errors) but giving me wrong answer of the multiplied matrix. The two matrices which are to be multiplied have been defined properly. Here's the code:

// position_new = multiply ( transformation (3x3) * transpose(3x1)  )
int rows=3, columns=1;
double multiply[][] = new double[rows][columns];
double sum;

for (int k = 0; k < columns; k++) {
    for (int e = 0; e < rows; e++) {
        sum = 0;
        for (int f = 0; f < columns; f++) {
            sum = sum + transformation[e][f] * transpose[f][k];
        }
        multiply[e][k] = sum;
    }
}
System.out.println("Multiplied Matrix:-");
for (int m = 0; m < rows; m++) {
    for (int n = 0; n < columns; n++)
        System.out.print(multiply[m][n] + "\t");
    System.out.print("\n");
} 

Thanks a lot for the help in advance.

1
  • looks like some code is missing Commented Apr 7, 2014 at 11:46

2 Answers 2

1
public void Miltiply(){
    // position_new = multiply ( transformation (3x3) * transpose(3x1)  )
    int rows=3, columns=1;
    int transformation[][]={{1,2,3},{1,1,1},{2,2,2}};
    int transpose[][]={{1},{1},{1}};
    double multiply[][] = new double[rows][columns];
    double sum;

    for (int k = 0; k < transpose[0].length; k++) {
        for (int e = 0; e < transformation.length; e++) {
            sum = 0;
            for (int f = 0; f < transpose.length; f++) {
                sum = sum + transformation[e][f] * transpose[f][k];
            }
            multiply[e][k] = sum;
        }
    }
    System.out.println("Multiplied Matrix:-");
    for (int m = 0; m < rows; m++) {
        for (int n = 0; n < columns; n++)
            System.out.print(multiply[m][n] + "\t");
        System.out.print("\n");
    } 
}

Output (M_3x3 X M_3x1 = M_3x1):

6.0 
3.0 
6.0
Sign up to request clarification or add additional context in comments.

Comments

0

your column/row variables are incorrect, it should be this:

// position_new = multiply ( transformation (mxn) * transpose(nxp)  )
int m = 3, n = 3, p = 1;
double multiply[][] = new double[m][p];
double sum;

for (int k = 0; k < p; k++) {
    for (int e = 0; e < m; e++) {
        sum = 0;
        for (int f = 0; f < n; f++) {
            sum = sum + transformation[e][f] * transpose[f][k];
        }
        multiply[e][k] = sum;
    }
}
System.out.println("Multiplied Matrix:-");
for (int i = 0; i < m; i++) {
    for (int j = 0; j < p; j++)
        System.out.print(multiply[i][j] + "\t");
        System.out.print("\n");
}  

With the example you gave you were only looping over the first column of the transformation matrix (because columns == 1) This means you were doing a 1x3 * 3x1 matrix multiplication. :)

2 Comments

Hi what does transpose.[0].length do? I am sorry a bit new to java. I have transpose matrix defined but when i pasted the code, it says "Syntax error on token ".", class expected after this token"
Sorry, my bad I have tidied it up to use variables again. @user3419155, that line gets the length of the second array dimension. You're probably missing transpose as a variable. Here is a mock up you can add:int transformation[][] = new int[m][n]; transformation[0][0] = 7; transformation[0][1] = 5; transformation[0][2] = 2; transformation[1][0] = 9; transformation[1][1] = 4; transformation[1][2] = 8; transformation[2][0] = 2; transformation[2][1] = 4; transformation[2][2] = 3; int transpose[][] = new int[n][p]; transpose[0][0] = 4; transpose[1][0] = 5; transpose[2][0] = 1; Giving: 55, 64, 31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.