1. Member
Join Date
Dec 2009
Posts
9
Rep Power
0

## 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();
}
}
}

2. Member
Join Date
Dec 2009
Posts
24
Rep Power
0
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];
}

}

3. You can't multiply two 2x3 matrixes.

kind regards,

Jos

4. Member
Join Date
Dec 2009
Posts
9
Rep Power
0
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??

5. Member
Join Date
Dec 2009
Posts
9
Rep Power
0
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!!!

6. Originally Posted by wtd_nielsen
you need to do something like this
Java 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.

7. Member
Join Date
Dec 2009
Posts
9
Rep Power
0
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??
Last edited by smart princess; 12-06-2009 at 07:43 PM.

8. 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:

Java 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

9. Member
Join Date
Dec 2009
Posts
9
Rep Power
0
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:

Java 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,,

#### Posting Permissions

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