【问题标题】:Matrix of matrices in JavaJava中的矩阵矩阵
【发布时间】:2020-06-10 04:33:39
【问题描述】:

我需要建立一个矩阵矩阵。例如,在一个 3x2 矩阵中,每个位置 (i, j) 也必须包含一个 2x2 矩阵。因此,对于这种情况,您将有一个由 6 个空格和 6 个子矩阵组成的矩阵,每个子矩阵有 4 个空格。这个想法是填充它然后显示它。

int mat [][];
mat = new int[x][y];
*for example* mat[0][0] = new int[2][2]; *this does not allow java*

如何解决?

【问题讨论】:

    标签: java arrays matrix


    【解决方案1】:

    你可以像下面这样在 Java 中使用四维矩阵,

    int [][][][]fourDimensional = new int[10][10][10][10];
    

    这个矩阵代表4维矩阵。如果您认为它像张量,那将很有用,它只不过是一个多维数组。

    【讨论】:

      【解决方案2】:
      int mat [][][][]=new int[3][2][2][2];
      

      在哪里,

      mat[0][0] is 2x2 matrix
      mat[0][1] is 2x2 matrix
      mat[0][2] is 2x2 matrix
      

      等等……

      【讨论】:

        【解决方案3】:

        矩阵是一个二维数组。因此,矩阵矩阵是一个 4D 数组。

        所以 2x2 矩阵的 3x2 矩阵将是:

        int[][][][] matrixOfMatrices = new int[3][2][2][2];
        

        至于如何打印,这里举个例子:

        int outerHeight = 3, outerWidth = 2, innerHeight = 2, innerWidth = 2;
        int[][][][] matrixOfMatrices = new int[outerHeight][outerWidth][innerHeight][innerWidth];
        
        // Fill with some data
        for (int i = 1, outRow = 0; outRow < outerHeight; outRow++)
            for (int outCol = 0; outCol < outerWidth; outCol++)
                for (int inRow = 0; inRow < innerHeight; inRow++)
                    for (int inCol = 0; inCol < innerWidth; inCol++)
                        matrixOfMatrices[outRow][outCol][inRow][inCol] = i++;
        
        // Print 2D x 2D matrix
        int numWidth = Integer.toString(outerHeight * outerWidth * innerHeight * innerWidth + 1).length();
        for (int outRow = 0; outRow < outerHeight; outRow++) {
            if (outRow != 0) {
                for (int outCol = 0; outCol < outerWidth; outCol++) {
                    if (outCol != 0)
                        System.out.print("-+-");
                    System.out.print("-".repeat(innerWidth * (numWidth + 1) - 1));
                }
                System.out.println();
            }
            for (int inRow = 0; inRow < innerHeight; inRow++) {
                for (int outCol = 0; outCol < outerWidth; outCol++) {
                    if (outCol != 0)
                        System.out.print(" | ");
                    for (int inCol = 0; inCol < innerWidth; inCol++) {
                        if (inCol != 0)
                            System.out.print(" ");
                        System.out.printf("%" + numWidth + "d", matrixOfMatrices[outRow][outCol][inRow][inCol]);
                    }
                }
                System.out.println();
            }
        }
        

        输出

         1  2 |  5  6
         3  4 |  7  8
        ------+------
         9 10 | 13 14
        11 12 | 15 16
        ------+------
        17 18 | 21 22
        19 20 | 23 24
        

        查看不同尺寸的打印结果:

        int outerHeight = 2, outerWidth = 3, innerHeight = 4, innerWidth = 5;
        

        输出

          1   2   3   4   5 |  21  22  23  24  25 |  41  42  43  44  45
          6   7   8   9  10 |  26  27  28  29  30 |  46  47  48  49  50
         11  12  13  14  15 |  31  32  33  34  35 |  51  52  53  54  55
         16  17  18  19  20 |  36  37  38  39  40 |  56  57  58  59  60
        --------------------+---------------------+--------------------
         61  62  63  64  65 |  81  82  83  84  85 | 101 102 103 104 105
         66  67  68  69  70 |  86  87  88  89  90 | 106 107 108 109 110
         71  72  73  74  75 |  91  92  93  94  95 | 111 112 113 114 115
         76  77  78  79  80 |  96  97  98  99 100 | 116 117 118 119 120
        

        【讨论】:

          【解决方案4】:

          您可以通过收藏列表来实现这一点。如下所示。下面的代码创建一个矩阵列表。与拥有多维数组相比,这更易于维护。

              List<int[][]> matrixArray = new ArrayList<int[][]>();
              int[][] a1= new int[2][2];
              int[][] a2= new int[2][2];
              int[][] a3= new int[2][2];
              int[][] a4= new int[2][2];
              matrixArray.add(a1);
              matrixArray.add(a2);
          

          【讨论】:

          • 那是 3x2 矩阵的列表,而不是 2x2 矩阵的 3x2 矩阵。
          猜你喜欢
          • 1970-01-01
          • 2018-04-02
          • 1970-01-01
          • 1970-01-01
          • 2011-12-18
          • 1970-01-01
          • 2019-07-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多