簡體   English   中英

如何在 Java 中加入 3 個矩陣?

[英]How can I join 3 matrices in Java?

我想在 Java 中加入下面顯示的這 3 個矩陣。 有沒有辦法讓我這樣做,比如循環或其他什么? 任何方法都可以。

//This where i define the array for the matrix
int[] MA1 = new int[]{1, 2, 4};
int[] MA2 = new int[]{3, 7, 2};
int[] MA3 = new int[]{5, 8, 4};

int[] MB1 = new int[]{1, 2, 2};
int[] MB2 = new int[]{2, 2, 1};
int[] MB3 = new int[]{1, 1, 1};

int[] MC1 = new int[]{11, 14, 12};
int[] MC2 = new int[]{12, 45, 30};
int[] MC3 = new int[]{23, 45, 12};

// This is where I unify the arrays to a set
// so I can call it as a matrix on my looping
int[][] MatrixA = new int[][]{MA1, MA2, MA3};
int[][] MatrixB = new int[][]{MB1, MB2, MB3};
int[][] MatrixC = new int[][]{MC1, MC2, MC3};

這樣結果將如下所示:

1    2    4    3    7    2    5    8    4
1    2    2    2    2    1    1    1    1
11   14   12   12   45   30   23   45   12

我試過這樣的事情:

int[][] MatrixD = new int[][]{
        MA1, MB1, MC1,
        MA2, MB2, MC2,
        MA3, MB3, MC3};

System.out.println("Matrix D");
for (int x = 0; x < MatrixD.length; x++) {
    for (int y = 0; y < MatrixD[x].length; y++) {
        System.out.print(MatrixD[x][y] + " ");
    }
    System.out.println();
}

但結果是:

1    2    4 
1    2    2 
11   14   12 
3    7    2 
2    2    1 
12   45   30 
5    8    4 
1    1    1 
23   45   12

似乎您想展平 3 個輸入矩陣,即將 3x3 矩陣轉換為 9 個值的數組,因此首先為此編寫一個方法。 這是您需要System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)輔助方法的地方。

// Flatten matrix, e.g.
//   {{1,2,3},
//    {4,5,6},   ==>   {1,2,3,4,5,6,7,8,9}
//    {7,8,9}}
static int[] flatten(int[][] matrix) {
    int height = matrix.length, width = matrix[0].length;
    int[] flat = new int[height * width];
    for (int i = 0; i < height; i++) {
        System.arraycopy(matrix[i], 0, flat, i * width, width);
    }
    return flat;
}

現在您可以通過簡單地展平輸入矩陣來合並它們:

static int[][] merge(int[][]... matrices) {
    int[][] result = new int[matrices.length][];
    for (int i = 0; i < matrices.length; i++) {
        result[i] = flatten(matrices[i]);
    }
    return result;
}

測試

int[][] MatrixA = { {1, 2, 4},
                    {3, 7, 2},
                    {5, 8, 4} };
int[][] MatrixB = { {1, 2, 2},
                    {2, 2, 1},
                    {1, 1, 1} };
int[][] MatrixC = { {11, 14, 12},
                    {12, 45, 30},
                    {23, 45, 12} };
print(MatrixA);
print(MatrixB);
print(MatrixC);

int[][] merged = merge(MatrixA, MatrixB, MatrixC);
print(merged);
static void print(int[][] matrix) {
    for (int[] row : matrix) {
        System.out.println(Arrays.toString(row));
    }
    System.out.println();
}

Output

[1, 2, 4]
[3, 7, 2]
[5, 8, 4]

[1, 2, 2]
[2, 2, 1]
[1, 1, 1]

[11, 14, 12]
[12, 45, 30]
[23, 45, 12]

[1, 2, 4, 3, 7, 2, 5, 8, 4]
[1, 2, 2, 2, 2, 1, 1, 1, 1]
[11, 14, 12, 12, 45, 30, 23, 45, 12]

與溪流...

為方便起見定義這兩個函數

public static int[] concat(int[] a, int[] b) {
    return IntStream.concat(Arrays.stream(a), Arrays.stream(b)).toArray();
}

public static int[] concat(int[] a, int[] b, int[] c) {
    return concat(concat(a, b), c);
}

現在你可以簡單地寫你的大矩陣

int[][] bigMatrix = new int[][] {concat(MA1,MA2,MA3), 
                                 concat(MB1,MB2,MB3), 
                                 concat(MC1,MC2,MC3)};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM