Arkan
Arkan

Reputation: 65

How can I join 3 matrices in Java?

I want to join these 3 matrices shown below in Java. Is there a way for me to do so like looping or something? Any method will be fine.

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

So that the result will look like this:

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

I've tried something like this:

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

But the result was:

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

Upvotes: 1

Views: 303

Answers (2)

karakfa
karakfa

Reputation: 67507

with streams...

define these two functions for convenience

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

now you can write your big matrix simply as

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

Upvotes: 1

Andreas
Andreas

Reputation: 159135

Seems you want to flatten the 3 input matrices, i.e. turn a 3x3 matrix into an array of 9 values, so start by writing a method for that. This is where you will need the System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) helper method.

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

Now you can merge the input matrices by simply flattening them:

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

Test

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]

Upvotes: 1

Related Questions