【问题标题】:Why is the output of my multiplication of 2D array not correct?为什么我的二维数组乘法的输出不正确?
【发布时间】:2020-11-19 14:49:41
【问题描述】:

我必须通过 2D 数组的乘法在 graph 中找到 distances,并且我正在尝试将图形的数组相乘,它可以工作,但是输出不正确。 而且我不知道问题出在哪里!

public class Graph {
    private int[][] AdjacencyMatrix = {
            {0, 1, 0, 1, 0},
            {1, 0, 1, 0, 1},
            {0, 1, 0, 1, 0},
            {1, 0, 1, 0, 0},
            {0, 1, 0, 0, 0}};

    int size = AdjacencyMatrix.length;
    private int[][] distanceMatix = new int[size][size];

    public void multiply() {
        int sum = 0;
        //für alle Zeilen in this matrix
        for (int row = 0; row < size; row++) {
            //für alle Spalten in other matrix
            for (int col = 0; col < size; col++) {
                //this matrix -> für alle Zellen in der Zeile
                //other matrix -> für alle Zellen in der Spalte
                for (int index = 0; index < size; index++) {
                    if (row == col) {
                        distanceMatix[row][col] = 0;
                    } else {
                        distanceMatix[row][col] +=
                                AdjacencyMatrix[row][index] *
                                        AdjacencyMatrix[index][col];
                    }
                }
            }
        }

        System.out.println("\n-------- Print DistanceMatrix --------\n");
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(distanceMatix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

我的输出:

0 0 2 0 1 
0 0 0 2 0 
2 0 0 0 1 
0 2 0 0 0 
1 0 1 0 0 

正确的输出是这样的:

0 1 2 1 2
1 0 1 2 1
2 1 0 1 2
1 2 1 0 3
2 1 2 3 0

【问题讨论】:

    标签: java arrays matrix multidimensional-array matrix-multiplication


    【解决方案1】:

    你的内部循环应该是这样的。原始数组在创建时初始化为 0's,您不想在 row == col 时跳过迭代。

    for (int index = 0; index <  size; index++) {  
          distanceMatix[row][col] += AdjacencyMatrix[row][index] * 
                                     AdjacencyMatrix[index][col];
    }
    
    

    我用过

    1 2
    3 4
    

    然后用手相乘得到

    7 10
    15 22
    

    与您的程序与上述更改相匹配。

    关于您的预期答案,您的输入矩阵可能是错误的。你可以验证你的答案here

    【讨论】:

      【解决方案2】:

      您可以使用通用的matrix multiplication 方法,这样您就不会感到困惑:

      int size = 5;
      int[][] matrix = {
              {0, 1, 0, 1, 0},
              {1, 0, 1, 0, 1},
              {0, 1, 0, 1, 0},
              {1, 0, 1, 0, 0},
              {0, 1, 0, 0, 0}};
      
      int[][] result = new int[size][size];
      for (int row = 0; row < size; row++)
          for (int col = 0; col < size; col++)
              for (int index = 0; index < size; index++)
                  result[row][col] += matrix[row][index] * matrix[index][col];
      
      // output
      for (int[] res : result) System.out.println(Arrays.toString(res));
      //[2, 0, 2, 0, 1]
      //[0, 3, 0, 2, 0]
      //[2, 0, 2, 0, 1]
      //[0, 2, 0, 2, 0]
      //[1, 0, 1, 0, 1]
      

      另见:An efficient way to multiply two object matrixes

      【讨论】:

        猜你喜欢
        • 2013-01-10
        • 1970-01-01
        • 1970-01-01
        • 2017-07-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多