Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Matrix multiplication problem with different dimensions

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Matrix multiplication problem with different dimensions

    Hi I'm trying to finish my program where I can either sum or multiply a matrix. The issue I'm having atm is that I can't figure out how to multiply them if I have 4 variables, 2 for the size in row and column in the first matrix, and another 2 for the row and size in the second matrix. This is the complete code:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    public class MatrizVaria{
     
        public static int x,y,o,p;
        public static BufferedReader var = new BufferedReader(new InputStreamReader(System.in));
     
        public void Fill(int a[][]){
            int i,j;
            for (i=0;i<x;i++)
                for (j=0;j<y;j++)
                    a[i][j] = (int)(Math.random()*10+1);
        }
        public void Sum(int a[][],int b[][],int c[][]){
            int i,j;
            for (i=0;i<x;i++)
                for (j=0;j<y;j++)
                    c[i][j] = a[i][j] + b[i][j];
        }
     
        public void Product(int a[][], int b[][], int c[][]) {
            int i, j, k, l;
            for (i=0;i<x;i++) {
                for (j=0;j<y;j++) {
                    c[i][j] = 0;
                    for (k=0;k<x;k++) {
                    for (l=0;l<y;l++) {
                    	c[i][j] += a[i][x]*b[x][j];
                    	c[i][j] += a[y][j]*b[i][y];                	
                    	}
                    }
                }
            }
        }
        public void Print(int a[][]){
            int i,j;
            for (i=0;i<x;i++){
                for (j=0;j<y;j++)
                    System.out.print("\t" + a[i][j]);
                	System.out.println();
            }
        }
        public static void main(String[] arg) throws Exception {
     
        	String carac;
     
        	do
    		{
    			System.out.print("\nDo you want to Sum(s) or Multiply(m) a matrix?: ");
    			carac = var.readLine();
    			if (carac.equals("s"))
    				{
    				try{
    					System.out.println("Input the row size: ");
    			        x = Integer.parseInt(var.readLine());
    			        System.out.println("Input the column size: ");
    			        y = Integer.parseInt(var.readLine());
     
    					int[][] Mat = new int[x][y];
    			        int[][] Mat1 = new int[x][y];
    			        int[][] Mat2 = new int[x][y];
    			        MatrizVaria obj = new MatrizVaria();
    			        obj.Fill(Mat);
    			        obj.Fill(Mat1);
    			        obj.Sum(Mat,Mat1,Mat2);
     
    			        System.out.println("The first Matrix is: ");
    			        obj.Print(Mat);
    			        System.out.println("The second Matrix is: ");
    			        obj.Print(Mat1);
    			        System.out.println("The sum of the Matrixes is : ");
    			        obj.Print(Mat2);
    				}catch(NumberFormatException nfe){
    					System.out.println(" Incorrect Input ");
    				}
    			}
    			if (carac.equals("m"))
    				{
    				try{
    					System.out.println("Input the row size of the first Matrix: ");
    			        x = Integer.parseInt(var.readLine());
    			        System.out.println("Input the column size of the first Matrix: ");
    			        y = Integer.parseInt(var.readLine());
    			        System.out.println("Input the row size of the second Matrix: ");
    			        o = Integer.parseInt(var.readLine());
    			        System.out.println("Input the column size of the second Matrix: ");
    			        p = Integer.parseInt(var.readLine());
     
    			        int[][] Mat = new int[x][y];
    			        int[][] Mat1 = new int[o][p];
    			        int[][] Mat2 = new int[x][y];
    			        MatrizVaria obj = new MatrizVaria();
    			        obj.Fill(Mat);
    			        obj.Fill(Mat1);
    			        obj.Product(Mat,Mat1,Mat2);
     
    			        System.out.println("The first Matrix is: ");
    			        obj.Print(Mat);
    			        System.out.println("The second Matrix is: ");
    			        obj.Print(Mat1);
    			        System.out.println("The product of both Matrixes is: ");
    			        obj.Print(Mat2);
    					}catch(NumberFormatException nfe){
    						System.out.println(" Incorrect Input ");
    				}
    			}
    			else
    			{
    				System.out.print("Invalid Option");
    			}
     
    			System.out.print("\nWould you like to perform another operation (y/n)?: ");
    			carac = var.readLine();
    			}
    			while(carac.equals("y"));
    			if(carac.equals("n"));
    			System.exit(0);
    			}
    }

    The part of it i want to finish is:

        public void Product(int a[][], int b[][], int c[][]) {
            int i, j, k, l;
            for (i=0;i<x;i++) {
                for (j=0;j<y;j++) {
                    c[i][j] = 0;
                    for (k=0;k<x;k++) {
                    for (l=0;l<y;l++) {
                    	c[i][j] += a[i][x]*b[x][j];
                    	c[i][j] += a[y][j]*b[i][y];                	
                    	}
                    }
                }
            }
        }
    if (carac.equals("m"))
    				{
    				try{
    					System.out.println("Input the row size of the first Matrix: ");
    			        x = Integer.parseInt(var.readLine());
    			        System.out.println("Input the column size of the first Matrix: ");
    			        y = Integer.parseInt(var.readLine());
    			        System.out.println("Input the row size of the second Matrix: ");
    			        o = Integer.parseInt(var.readLine());
    			        System.out.println("Input the column size of the second Matrix: ");
    			        p = Integer.parseInt(var.readLine());
     
    			        int[][] Mat = new int[x][y];
    			        int[][] Mat1 = new int[o][p];
    			        int[][] Mat2 = new int[x][y];
    			        MatrizVaria obj = new MatrizVaria();
    			        obj.Fill(Mat);
    			        obj.Fill(Mat1);
    			        obj.Product(Mat,Mat1,Mat2);
     
    			        System.out.println("The first Matrix is: ");
    			        obj.Print(Mat);
    			        System.out.println("The second Matrix is: ");
    			        obj.Print(Mat1);
    			        System.out.println("The product of both Matrixes is: ");
    			        obj.Print(Mat2);
    					}catch(NumberFormatException nfe){
    						System.out.println(" Incorrect Input ");
    				}
    			}

    Please help me, I really need to finish this part. Thank you


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Matrix multiplication problem with different dimensions

    I can't figure out how to multiply them if
    Do you have the algorithm for solving your problem?
    Once you get that, someone can help you code it in java.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Matrix multiplication problem with different dimensions

    Quote Originally Posted by Norm View Post
    Do you have the algorithm for solving your problem?
    Once you get that, someone can help you code it in java.
    I know that in a matrix of n sides and n columns, n being the same number obviously, then the output for the class Product would be this:

        public void Product(int a[][], int b[][], int c[][]) {
            int i, j, k;
            for (i = 0; i < n; i++) {
     
                for (j = 0; j < n; j++) {
                    c[i][j] = 0;
                    for (k = 0; k < n; k++) {
     
                        c[i][j] += a[i][k] * b[k][j];

    And the result I've confirmed it to be correct. However I'm having trouble figuring out how to implement this to different size matrixes.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Matrix multiplication problem with different dimensions

    You need a definition for how to multiply different sized matrixes.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Matrix multiplication problem with different dimensions

    Quote Originally Posted by Norm View Post
    You need a definition for how to multiply different sized matrixes.
    its like this:

    matrixproduct.jpg

    sorry for the crappy image. You multiply the row of the first matrix by the column of the second.

    If the number of columns of the 1st matrix doesn't match the number of rows of the 2nd matrix then the product is not possible.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Matrix multiplication problem with different dimensions

    Have you written it down showing the r,c for the input matrixes and the output matrixes to get the pattern for writing the loops?
    Something like this (I'm making up the indexes):
    0,0 = 0,0 * 0,0
    0,1 = 0,1 * 1,0

    Your posted image shows the contents of the matrix vs the indexes for contents.

Similar Threads

  1. How to make my Program always run with dimensions 1350 x 650?
    By i3riank in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 29th, 2011, 07:12 AM
  2. Having problem in matrix multiplication....
    By sidhant in forum Java Theory & Questions
    Replies: 5
    Last Post: March 17th, 2011, 01:41 PM
  3. Inverse of a Matrix problem
    By shivamchauhan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 17th, 2011, 01:40 PM
  4. Recursive multiplication and Karatsuba
    By jsp01 in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 14th, 2011, 02:04 AM
  5. Multiplication code
    By bomboy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 5th, 2011, 02:25 AM