Results 1 to 9 of 9

Thread: matrix multiply

  1. #1
    slixtrix is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default matrix multiply

    im doing a project for my class, and this is my code that i am using..

    i am currently having errors in this code which are:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at arrayMatrix.multMatrix(arrayMatrix.java:67)
    at arrayMatrix.main(arrayMatrix.java:32)

    i am using Eclipse to type this code, so if anyone can help me fix these problems i would greatly appreciate it



    Java Code:
    /* This program multiplies 2 matrices times each other.
     * If the matrices are not compatible, it warns the user. */
    public class arrayMatrix
    {
    	
    	//static List list;
    	
    	public static void main(String[] args)
    	{
    		int j, k, l;
    		int a[][], b[][], ab[][];
    		
    		System.out.println("Hello TV land!");
    		j = 1;
    		k = 2;
    		l = 3;
    		
    		a = makeIdent(2, 3);
    		a[0][0] = 1;a[0][1] = 2;a[0][2] = 4;
    		a[1][0] = 2;a[1][1] = 6;a[1][2] = 0;
    		showMatrix(2, 3, a, j);
    		
    		b = makeIdent(3, 4);
    		b[0][0] = 4;b[0][1] = 1;b[0][2] = 4;b[0][3] = 3;
    		b[1][0] = 0;b[1][1] = -1;b[1][2] = 3;b[1][3] = 1;
    		b[2][0] = 2;b[2][1] = 7;b[2][2] = 5;b[2][3] = 2;
    		showMatrix(3, 4, b, k);
    		
    		ab = multMatrix(2, 3, a, 3, 4, b);
    		showMatrix(2, 4, ab, l);
    		
    		
    	}
    	
    	
    	static int[][] makeIdent(int nRows, int nCols){
    		int a[][] = new int[nRows][nCols]; 
    		
    		return a;
    	}
    	static void showMatrix(int nRows, int nCols, int m[][], int x){
    		System.out.println("Matrix " + x + ":");
    		for(int i = 0; i < nRows; i++) {
    			for(int j = 0; j < nCols; j++) {
    		    	  System.out.println("Row: " + i + " Column: " + j
    		    			              + " = "+ m[i][j]);
    		    }
    		}
    	}
    	
    	 
    	
    	static int[][] multMatrix(int aRows, int aCols, int a[][], 
    	                          int bRows, int bCols, int b[][]){
    		int mult[][] = new int[aRows][bCols];
    		
    		if(aCols != bRows){
    			System.out.println("These matrices are not compatible");
    		}else{
    			for(int i = 0; i < aRows; i++) {
    			      for(int j = 0; j < bCols-1; j++) {
    			        for(int k = 0; k < bCols; k++){
    			          
    			          mult[i][j] += a[i][k]*b[k][j];
    			        }
    			      }  
    			     }
    			
    		}	
    		return mult;
    			}
    			
    		
    		
    		
    	}

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    a[i].length and b.length are only 3 each, and k has a value of 3, which means it's trying to read the fourth element.

  3. #3
    slixtrix is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    how do i fix that? maybe j < bCols instead of bCols -1?

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    No--instead of guessing, think about this logically.

    Your k value is going too high--it should go 2 instead of 3. Changing the j values will not fix this. Instead, you should be focused on this line:
    Java Code:
    for(int k = 0; k < bCols; k++){
    You're going to want to change something in that line.

    Edit: Fixed incorrect numerical values above.

  5. #5
    slixtrix is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    from what im understanding, i should change
    Java Code:
    for(int j = 0; j < bCols - 1; j++) {
    			        for(int k = 0; k < bCols; k++){
    to

    f
    Java Code:
    or(int j = 0; j < bCols; j++) {
    			        for(int k = 0; k < bCols-1; k++){
    am i misunderstanding what im doing wrong?

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    When in doubt, give it a try!

    But yes, you are correct, assuming this is the desired output:
    Java Code:
    Hello TV land!
    Matrix 1:
    Row: 0 Column: 0 = 1
    Row: 0 Column: 1 = 2
    Row: 0 Column: 2 = 4
    Row: 1 Column: 0 = 2
    Row: 1 Column: 1 = 6
    Row: 1 Column: 2 = 0
    Matrix 2:
    Row: 0 Column: 0 = 4
    Row: 0 Column: 1 = 1
    Row: 0 Column: 2 = 4
    Row: 0 Column: 3 = 3
    Row: 1 Column: 0 = 0
    Row: 1 Column: 1 = -1
    Row: 1 Column: 2 = 3
    Row: 1 Column: 3 = 1
    Row: 2 Column: 0 = 2
    Row: 2 Column: 1 = 7
    Row: 2 Column: 2 = 5
    Row: 2 Column: 3 = 2
    Matrix 3:
    Row: 0 Column: 0 = 12
    Row: 0 Column: 1 = 27
    Row: 0 Column: 2 = 30
    Row: 0 Column: 3 = 0
    Row: 1 Column: 0 = 8
    Row: 1 Column: 1 = -4
    Row: 1 Column: 2 = 26
    Row: 1 Column: 3 = 0

  7. #7
    slixtrix is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    when i basically swap the J and K (bCols) and (bCols - 1) i get actual values for [0][3] and [1][3] instead of the 0 that shows up on yours

    I do greatly appreciate you helping me put my brain back on the tracks on what i need to fix

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Ah, yes, I didn't notice the change in the j statement (just the k statement). My apologies. Is the program now working as you expected?

  9. #9
    slixtrix is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. how to multiply numbers in rows and print it next to it
    By racewithferrari in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 06:24 PM
  2. multiply two matrixes
    By smart princess in forum New To Java
    Replies: 8
    Last Post: 12-06-2009, 06:43 PM
  3. 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
  4. How to multiply two matrices
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:50 PM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

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