2014-02-04 2 views
0

У меня есть код, который будет печатать таблицу из 2-мерных массивов. Проблема, с которой я столкнулся, заключается в том, что я совершенно не знаю, как размножаться и находить продукт массивов. Любая помощь приветствуется. БлагодаряКак умножить 2-мерные массивы? Матричное умножение

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 
+0

ли вы имеете в виду умножение матриц? http://en.wikipedia.org/wiki/Matrix_multiplication –

+0

да, извините, что сделал это ясно – user3201634

+0

http://stackoverflow.com/questions/17623876/matrix-multiplication-using-arrays – pezetem

ответ

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. Используйте надлежащее значение 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(); 
    } 
    } 
    } 
0

для тех, кто любит методы:

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 
Смежные вопросы