# multiply two matrixes

• 12-06-2009, 06:23 PM
smart princess
multiply two matrixes
Hello there,,
i wrote this programe to Add two Matrix but i didn't know how can i maltiply it pleace help me...

//This program to add two dimention
import java.util.*;
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int[][]x=new int[2][3];
int[][]y=new int[2][3];
int [][]z=new int [2][3];
for(int i=0; i<2; i++){
for(int j=0; j<3; j++){
System.out.println("For the First Matrex row" +i+ "column" +j);
x[i][j]=scan.nextInt();
System.out.println("For the scond Matrx row" +i+ "column" +j);
y[i][j]=scan.nextInt();
z[i][j]=0;
z[i][j]= x[i][j] + y[i][j];
}
}
System.out.println("The 1st matrix");
for(int i=0; i<2; i++){
for(int j=0; j<3; j++){
System.out.print(x[i][j]+"\t");
}
System.out.println();
}
System.out.println("***************************");
System.out.println("The 2nd Matrix");
for(int i=0; i<2; i++){
for(int j=0; j<3; j++){
System.out.print(y[i][j]+"\t");
}
System.out.println();
}
System.out.println("***************************");
System.out.println("The Agging Matrix");
for(int i=0; i<2; i++){
for(int j=0; j<3; j++){
System.out.print(z[i][j]+"\t");
}
System.out.println();
}
}
}
• 12-06-2009, 06:33 PM
wtd_nielsen
you need to do something like this
for(int row = 0; row<matrix1.length; row++)
{
for(int column= 0; column<matrix1[row].length; column++)
{

matrix3[row][column] = matrix1[row][column]+matrix2[row][column];
}

}
• 12-06-2009, 06:52 PM
JosAH
You can't multiply two 2x3 matrixes.

kind regards,

Jos
• 12-06-2009, 06:59 PM
smart princess
Quote:

Originally Posted by wtd_nielsen
you need to do something like this
for(int row = 0; row<matrix1.length; row++)
{
for(int column= 0; column<matrix1[row].length; column++)
{

matrix3[row][column] = matrix1[row][column]+matrix2[row][column];
}

}

but do you mean that is my program is wrong??
and how can i multiply it??
• 12-06-2009, 07:03 PM
smart princess
Quote:

Originally Posted by JosAH
You can't multiply two 2x3 matrixes.

kind regards,

Jos

but note this is 2x3 multiply in 3x4 so how can not multiply it??
our teacher said any equation in descript mathematic we can solve it by java!!!
• 12-06-2009, 07:03 PM
Fubarable
Quote:

Originally Posted by wtd_nielsen
you need to do something like this
Code:

```for(int row = 0; row<matrix1.length; row++) {     for(int column= 0; column<matrix1[row].length; column++)     {           matrix3[row][column] = matrix1[row][column]+matrix2[row][column];     } }```

and if you could mult these matrices, this wouldn't be the way to do it as you would need 3 nested for loops to succeed.
• 12-06-2009, 07:11 PM
smart princess
Quote:

Originally Posted by Fubarable
and if you could mult these matrices, this wouldn't be the way to do it as you would need 3 nested for loops to succeed.

in fact i noted that, but could you tell me the right way to do it??
• 12-06-2009, 07:34 PM
JosAH
Quote:

Originally Posted by smart princess
in fact in noted that, but could you tell me the right way to do it??

Element P[i, j] of the product matrix A*B is the dot product of row A[i] and column C[j]. So a fragment of Java can be:

Code:

```for (int i= 0; i < A.length; i++)   for (int j= 0; j < B[i].length; j++)       P[i][j]= dot(A, B, i, j);```
Now all you have to do it implement the dot( ... ) method.

kind regards,

Jos
• 12-06-2009, 07:43 PM
smart princess
Quote:

Originally Posted by JosAH
Element P[i, j] of the product matrix A*B is the dot product of row A[i] and column C[j]. So a fragment of Java can be:

Code:

```for (int i= 0; i < A.length; i++)   for (int j= 0; j < B[i].length; j++)       P[i][j]= dot(A, B, i, j);```
Now all you have to do it implement the dot( ... ) method.

kind regards,

Jos

Thanxx
i will try it,,