遇到矩阵相乘时的异常错误。

huangapple go评论51阅读模式
英文:

Getting exception error when trying matrix multiplication

问题

以下是您要翻译的内容:

"Code works fine when both matrices have same number of rows and columns. like for eg. 3x3 and 3x3 matrix multiplication works fine.

Getting below error when trying to multiply matrices with different sizes. Tried multiplying a 2x3 matrix and a 3x4 matrix and got error."

英文:

Code works fine when both matrices have same number of rows and columns. like for eg. 3x3 and 3x3 matrix multiplication works fine.

Getting below error when trying to multiply matrices with different sizes. Tried multiplying a 2x3 matrix and a 3x4 matrix and got error.

**Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
	at arrayprac.ArrayPrac.main(ArrayPrac.java:85)
38C:\Users\a807771\AppData\Local\NetBeans\Cache\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\a807771\AppData\Local\NetBeans\Cache\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 12 seconds)**
package arrayprac;
import java.util.Scanner;

public class ArrayPrac {
 
    public static void main(String[] args) {
        
     System.out.println("Enter number of rows for matrix 1: ");
     int row1,col1,row2,col2;
     Scanner sc=new Scanner(System.in);
     row1=sc.nextInt();
     System.out.println("Enter number of Columns for matrix 1: ");
     col1=sc.nextInt();
     System.out.println("Enter number of rows for matrix 2: ");
     row2=sc.nextInt();
     System.out.println("Enter number of Columns for matrix 2: ");
     col2=sc.nextInt(); 
     int a[][]=new int[row1][col1];
     int b[][]=new int[row2][col2];
     for(int i=0;i<row1;i++)
     {
         for(int j=0;j<col1;j++)
         {
             System.out.println("Enter element for 1st matrix: ");
             a[i][j]=sc.nextInt();
         }
        
     }
     for(int i=0;i<row2;i++)
     {
         for(int j=0;j<col2;j++)
         {
             System.out.println("Enter element for 2nd matrix: ");
             b[i][j]=sc.nextInt();
         }
        
     }
     System.out.println("Below is the 1st matrix: \n");
     for(int i=0;i<row1;i++)
     {
         for(int j=0;j<col1;j++)
         {
             System.out.print(a[i][j]);
             
         }
        System.out.println("");
     }
     
     System.out.println("Below is the 2nd matrix: \n");
     for(int i=0;i<row2;i++)
     {
         for(int j=0;j<col2;j++)
         {
             System.out.print(b[i][j]);
             
         }
        System.out.println("");
     }
     
     if(col1!=row2)
     {
         System.out.println("Matrix multiplication impossible!");
         System.exit(0);
     }
     else{
         int c[][]=new int[row1][col2];
         
         for(int i=0;i<row1;i++)
         {
             for(int j=0;j<col2;j++)
             {
                 c[i][j]=0;
                 for(int k=0;k<row2;k++)
                 {
                     c[i][j]=c[i][j]+a[i][k]*b[k][j];
                 }
             }
         }
         
     System.out.println("Below is the result matrix: \n");
     for(int i=0;i<col1;i++)
     {
         for(int j=0;j<row2;j++)
         {
             System.out.print(c[i][j]);
             
         }
        System.out.println("");
     }
     }   
     
    }
    
}

Code works fine when both matrices have same number of rows and columns. like for eg. 3x3 and 3x3 matrix multiplication works fine.

Getting error when trying to multiply matrices with different sizes. Tried multiplying a 2x3 matrix and a 3x4 matrix and got error.

答案1

得分: 1

The iteration bounds in the double for loop for printing the new matrix should match the previous for loops for multiplication. I think this correction in your code will make it work.

for(int i=0;i<row1;i++)//Here
{
    for(int j=0;j<col2;j++)//And here
    {
        System.out.print(c[i][j]);
    }
    System.out.println("");
}

Generally speaking, in situations like this it will help if you set breakpoints in the lines where the problem is and then using the debugger.

英文:

The iteration bounds in the double for loop for printing the new matrix should match the previous for loops for multiplication. I think this correction in your code will make it work.

for(int i=0;i&lt;row1;i++)//Here
        {
            for(int j=0;j&lt;col2;j++)//And here
            {
                System.out.print(c[i][j]);

            }
            System.out.println(&quot;&quot;);
        }

Generally speaking, in situations like this it will help if you set breakpoints in the lines where the problem is and then using the debugger.

huangapple
  • 本文由 发表于 2023年5月13日 12:20:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定