2015-01-04 68 views
0

我是一個完整的Java新手,我試圖乘以兩個二維數組,如你將乘以兩個矩陣。下面的程序只適用於方矩陣,但不適用於其他程序。我似乎無法弄清楚我出錯的地方。如果有人能幫助我,那會很好。試圖乘二維數組(如在矩陣乘法),但只適用於矩陣矩陣

import java.util.Scanner; 

public class TwoDMatrix { 
    public static void main (String [] args){ 
     Scanner scanme = new Scanner(System.in); 

     //Input dimensions of Matrix A 
     System.out.println("Enter the dimensions (row x column) of Matrix A"); 
     int rowA = scanme.nextInt(); 
     int columnA = scanme.nextInt(); 
     int [][] matA = new int [rowA][columnA]; 



     //Input dimensions of Matrix B 
     System.out.println("Enter the dimensions (row x column) of Matrix B"); 
     int rowB = scanme.nextInt(); 
     int columnB = scanme.nextInt(); 
     int [][] matB = new int [rowB][columnB]; 



     // Declaring new variables 
     int [][] product = new int [columnA][rowB]; 
     int rowCountA, columnCountA, rowCountB, columnCountB; 
     int rowCountProduct, columnCountProduct; 
     int sum; 
     String divider = "---------"; 



     // Input values of Matrix A 
     for (rowCountA = 0; rowCountA < rowA; rowCountA++){ 
      for (columnCountA = 0; columnCountA < columnA; columnCountA++){ 
       System.out.printf("%s%d%s%d%s", "Enter the value at A(", rowCountA, ",", columnCountA, ")"); 
       matA[rowCountA][columnCountA] = scanme.nextInt(); 
      } 
     } 



     // Input values of Matrix B 
     for (rowCountB = 0; rowCountB < rowB; rowCountB++){ 
      for (columnCountB = 0; columnCountB < columnB; columnCountB++){ 
       System.out.printf("%s%d%s%d%s", "Enter the value at B(", rowCountB, ",", columnCountB, ")"); 
       matB[rowCountB][columnCountB] = scanme.nextInt(); 
      } 
     } 



     //Calculate product of the two matrices 
     for (rowCountProduct = 0; rowCountProduct < rowA; rowCountProduct++){ 
      for (columnCountProduct = 0; columnCountProduct < columnB; columnCountProduct++){ 
       sum = 0; 
       for (columnCountA=0, rowCountB=0; columnCountA<columnA && rowCountB<rowB; columnCountA++, rowCountB++){ 
        sum += (matA[rowCountProduct][columnCountA] * matB[rowCountB][columnCountProduct]); 
       } 
       product[rowCountProduct][columnCountProduct] = sum; 
      } 
     } 



     //Prints the input matrix A 
     System.out.printf("%n%s%n%s%n", "Matrix A:", divider); 
     for (rowCountA = 0; rowCountA < rowA; rowCountA++){ 
      for (columnCountA = 0; columnCountA < columnA; columnCountA++){ 
       System.out.printf("%5d", matA[rowCountA][columnCountA]); 
      } 
      System.out.println(); 
     } 



     //Prints the input matrix B 
     System.out.printf("%n%s%n%s%n", "Matrix B:", divider); 
     for (rowCountB = 0; rowCountB< rowB; rowCountB++){ 
      for (columnCountB = 0; columnCountB < columnB; columnCountB++){ 
       System.out.printf("%5d", matB[rowCountB][columnCountB]); 
      } 
      System.out.println(); 
     } 



     //Prints the product 
     System.out.printf("%n%s%n%s%n", "Product", divider); 
     for (rowCountProduct = 0; rowCountProduct < rowA; rowCountProduct++){ 
      for (columnCountProduct = 0; columnCountProduct < columnB; columnCountProduct++){ 
       System.out.printf("%5d", product[rowCountProduct][columnCountProduct]); 
      } 
      System.out.println(); 
     } 
    } 
} 
+2

你這是什麼意思是「它不工作」?拋出任何異常或錯誤的結果? –

+0

如果兩個矩陣都是正方形且具有相同的尺寸,那麼程序工作得很好。例如: 矩陣A是2×2,矩陣B是2×2,或者 矩陣A是3×3,矩陣B是3×3。 在這些條件下它工作正常。 但是如果 矩陣A是2x3並且矩陣B是3x2或 矩陣A是1x2並且矩陣B是2x3, 在這些條件下,它給出了arrayIndexOutOfBound錯誤。 –

+0

您是否嘗試應用我給您的僞代碼? – Dici

回答

0

基本上,你定義你的產品矩陣:

int [][] product = new int [columnA][rowB]; 

這意味着,因爲在B.

行,應該有儘可能多的行,也有在A柱,併爲多列

但是,當你循環來填充它,這是你的循環:

for (rowCountProduct = 0; rowCountProduct < rowA; rowCountProduct++){ 
     for (columnCountProduct = 0; columnCountProduct < columnB; columnCountProduct++){ 
     ... 
     } 
    } 

這意味着,你牛逼試圖填充產品中的行,這些行應該在範圍內0rowCountProduct < columnA其值在範圍0rowCountProduct < rowA。同樣,按照您的定義,將列運行到範圍columnB而不是rowB

所以你應該改變矩陣的定義,或者改變你填充矩陣的方式。

1

自從我學習線性代數已經有一段時間了,但是我認爲當你用矩陣B [n2] [m2]乘一個矩陣A [n1] [m1]時,m1必須等於n2,結果應該是矩陣C [n1] [m2]。

因此

int [][] product = new int [columnA][rowB]; 

應該

int [][] product = new int [rowA][columnB]; 

而且你應該確認columnA == rowB啓動乘法之前。

1

你有一個相當複雜的條件在這裏:

for (columnCountA=0, rowCountB=0; columnCountA<columnA && rowCountB<rowB; columnCountA++, rowCountB++){ 
    sum += (matA[rowCountProduct][columnCountA] * matB[rowCountB][columnCountProduct]); 
} 

記住數學公式:

be A a n x l matrix, B a l x m matrix, then 
forall (i,j) in [1,n]x[1,m], (AB)(i,j) = sum_(k in [1,l]) { A(i,k).B(k,j) } 

因此,對於這樣的僞代碼:

for (int i=0 ; i<A.length ; i++) { 
    for (int j=0 ; j<B[0].length ; j++) { 
     prod[i][j] = 0; 
     for (int k=0 ; k<A[0].length ; k++) { 
      prod[i][j] += A[i][k]*B[k][j]; 
     } 
    } 
}