Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default 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 class AddMatrex{
    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. #2
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    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. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    You can't multiply two 2x3 matrixes.

    kind regards,

    Jos

  4. #4
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by wtd_nielsen View Post
    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];
    }

    }
    first thanxx 4 ur reply,,,
    but do you mean that is my program is wrong??
    and how can i multiply it??

  5. #5
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

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

    kind regards,

    Jos
    thanxx for ur reply,,
    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. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by wtd_nielsen View Post
    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. #7
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    thanxx to reply,,
    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 06:43 PM.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by smart princess View Post
    thanxx to reply,,
    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. #9
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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,,

Similar Threads

  1. display column value multiply with 100
    By tiiim83 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-15-2009, 03:40 AM
  2. How to multiply two matrices
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:50 PM

Posting Permissions

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