【问题标题】:How to multiply 2 dimensional arrays? Matrix Multiplication如何将二维数组相乘?矩阵乘法
【发布时间】:2014-02-28 03:01:04
【问题描述】:

所以我有一个代码可以打印一个二维数组表。我遇到的问题是我完全不知道如何相乘并找到数组的乘积。任何帮助表示赞赏。谢谢

public class MultiplyingArrays {

    public static void main(String[] args) {
        int firstarray[][] = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
        int secondarray[][] = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};

        System.out.println("This is the first array");
        display(firstarray);

        System.out.println("This is the second array");
        display(secondarray);
    }

    public static void display(int x[][]) {
        for (int row = 0; row < x.length; row++) {
            for (int column = 0; column < x[row].length; column++) {
                System.out.print(x[row][column] + "\t");
            }
            System.out.println();
        }
    }
}

期望的结果是:

 -3   43
 18   60
  1  -20

【问题讨论】:

标签: java arrays matrix


【解决方案1】:
int firstarray[][] = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
int secondarray[][] = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};

/* Create another 2d array to store the result using the original arrays' lengths on row and column respectively. */
int [][] result = new int[firstarray.length][secondarray[0].length];

/* Loop through each and get product, then sum up and store the value */
for (int i = 0; i < firstarray.length; i++) { 
    for (int j = 0; j < secondarray[0].length; j++) { 
        for (int k = 0; k < firstarray[0].length; k++) { 
            result[i][j] += firstarray[i][k] * secondarray[k][j];
        }
    }
}
/* Show the result */
display(result);

附:使用正确的naming convention

【讨论】:

    【解决方案2】:
           import java.util.Scanner;
           class MatrixMultiplication
      {
           public static void main(String args[])
      {
           int n;
           Scanner sc=new Scanner(System.in);
           System.out.println("Enter the base of square matrix");
           n=sc.nextInt();
           int a[][]=new int[n][n];
           int b[][]=new int[n][n];
           int c[][]=new int[n][n];
           System.out.println("Enter the elements of matrix a");
           for(int i=0;i<n;i++)
        {
           for(int j=0;j<n;j++)
        {
           a[i][j]=sc.nextInt();
        }
        }
           System.out.println("Enter the elements of matrix b");
           for(int i=0;i<n;i++)
        {
           for(j=0;j<n;j++)
        {
           b[i][j]=sc.nextInt();
        }
        }
           System.out.println("Multiplying the matrices....");
        {
           for(int i=0;i<n;i++)
        {
           for(int j=0;j<n;j++)
        {
           for(int k=0;k<n;k++)
        {
           c[i][j]=c[i][j]+a[i][k]*b[k][j];
        }
        }
        }
           System.out.println("the product is:");
           for(int i=0;i<n;i++)
        {
           for(int j=0;j<n;j++)
        {
           System.out.print(c[i][j]+"   ");
        }
           System.out.println();
        }
        }
        }
    

    【讨论】:

      【解决方案3】:

      对于那些喜欢方法的人:

      import java.util.*;
      
      public class MatmultD
      {
      private static Scanner sc = new Scanner(System.in);
        public static void main(String [] args)
        {
          int a[][] = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
          int b[][] = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};
          int[][] c=multMatrix(a,b);
          printMatrix(a);
          printMatrix(b);    
          printMatrix(c);
      
        }
      
         public static int[][] readMatrix() {
             int rows = sc.nextInt();
             int cols = sc.nextInt();
             int[][] result = new int[rows][cols];
             for (int i = 0; i < rows; i++) {
                 for (int j = 0; j < cols; j++) {
                    result[i][j] = sc.nextInt();
                 }
             }
             return result;
         }
      
      
        public static void printMatrix(int[][] mat) {
        System.out.println("Matrix["+mat.length+"]["+mat[0].length+"]");
             int rows = mat.length;
             int columns = mat[0].length;
             for (int i = 0; i < rows; i++) {
                 for (int j = 0; j < columns; j++) {
                     System.out.printf("%4d " , mat[i][j]);
                 }
                 System.out.println();
             }
         System.out.println();
        }
      
         public static int[][] multMatrix(int a[][], int b[][]){//a[m][n], b[n][p]
         if(a.length == 0) return new int[0][0];
         if(a[0].length != b.length) return null; //invalid dims
      
         int n = a[0].length;
         int m = a.length;
         int p = b[0].length;
         int ans[][] = new int[m][p];
      
         for(int i = 0;i < m;i++){
            for(int j = 0;j < p;j++){
               for(int k = 0;k < n;k++){
                  ans[i][j] += a[i][k] * b[k][j];
               }
            }
         }
         return ans;
         }
      }
      

      输出看起来像

      Matrix[3][4]
         1    2   -2    0 
        -3    4    7    2 
         6    0    3    1 
      
      Matrix[4][2]
        -1    3 
         0    9 
         1  -11 
         4   -5 
      
      Matrix[3][2]
        -3   43 
        18  -60 
         1  -20 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-27
        • 1970-01-01
        • 2018-04-11
        • 2021-08-14
        • 2021-12-26
        • 2023-01-04
        • 2016-06-20
        • 1970-01-01
        相关资源
        最近更新 更多