如何乘以二维数组?矩阵乘法

时间:2022-06-24 21:24:23

So I have a code that will print a table of 2 dimensional arrays. The problem that I've run into is that I have absolutely no idea how to multiply and find the product of the arrays. Any help is appreciated. Thanks

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

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();
        }
    }
}

The desired result would be:

期望的结果是:

 -3   43
 18   60
  1  -20

3 个解决方案

#1


6  

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);

P.S. Use proper naming convention.

附:使用适当的命名约定。

#2


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


0  

for those who like methods:

对于那些喜欢方法的人:

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;
   }
}

and the output look like

和输出看起来像

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 

#1


6  

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);

P.S. Use proper naming convention.

附:使用适当的命名约定。

#2


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


0  

for those who like methods:

对于那些喜欢方法的人:

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;
   }
}

and the output look like

和输出看起来像

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