1

I've tried following this one How do you append two 2D array in java properly? removing all of the array copys but something is wrong. I also tried another guide but that only worked if the rows were the same.

public int [][] appendMatrix(int[][]matrix, int [][] matrix2)
    {

        this.matrix = new int[matrix.length + matrix2.length][matrix[0].length];
        for(int i = 0; i < matrix.length; i++)
        {
            for(int j = 0; j < matrix[i].length; j++)
            {
                this.matrix[i][j] = matrix[i][j];
            }
            for(int j = matrix.length; j < matrix.length + matrix2.length; j++)
            {
                this.matrix[i][j]= matrix2[i-matrix.length][j];
            }   
        }
        return this.matrix;**
klion
  • 63
  • 4

1 Answers1

0

The important thing to consider is when we go from the final row in our first matrix, we want to keep that value so we can use it to add the first through n'th row of our second matrix to our resulting matrix, without losing track.

Example output: output as an array and visualized as a matrix

package matrix;

// I know you don't want to use imports, this is simply for testing purposes.
import java.util.Arrays;

public class MatrixAddition
{
    public static void main(String[] args)
    {
        int[][] matrix1 =
        {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 },
                { 10, 11, 12 } };
        int[][] matrix2 =
        {
                { 1, 1, 1 },
                { 2, 3, 4 } };


        System.out.println("Appending the two matrices results in: ");
        System.out.println(Arrays.deepToString(twoDMatrixAppend(matrix1, matrix2)));
        printMatrix(twoDMatrixAppend(matrix1, matrix2));

        System.out.println("\nPrepending the two matrices results in: ");
        System.out.println(Arrays.deepToString(twoDMatrixPrepend(matrix1, matrix2)));
        printMatrix(twoDMatrixPrepend(matrix1, matrix2));
    }


    private static int[][] twoDMatrixAppend(int[][] matrix1, int[][] matrix2)
    {
        if (matrix1[0].length != matrix2[0].length)
        {
            return null; // Or throw new incompatible matrices exception
        }

        int resultingRowLength = matrix1.length + matrix2.length; // The new length of the resulting matrix

        int[][] result = new int[resultingRowLength][matrix1[0].length];

        int currentRow, col, matrixTwoRowStart;
        for (currentRow = 0; currentRow < matrix1.length; currentRow++)
        {
            for (col = 0; col < matrix1[0].length; col++)
            {
                result[currentRow][col] = matrix1[currentRow][col];
            }
        }

        for (matrixTwoRowStart = 0; matrixTwoRowStart < matrix2.length; matrixTwoRowStart++, currentRow++)
        {
            for (col = 0; col < matrix2[0].length; col++)
            {
                result[currentRow][col] = matrix2[matrixTwoRowStart][col];
            }
        }

        return result;
    }

    private static int[][] twoDMatrixPrepend(int[][] matrix1, int[][] matrix2)
    {
        return twoDMatrixAppend(matrix2, matrix1);
    }

    private static void printMatrix(int[][] arr)
    {
        System.out.println();
        int row, col;
        for (row = 0; row < arr.length; row++)
        {
            for (col = 0; col < arr[0].length; col++)
            {
                System.out.print(String.format("%4d", arr[row][col]));
            }
            System.out.println();
        }
    }

}
Paper-SSheets
  • 333
  • 2
  • 8
  • No I need to just put to 2D array matrices together so matrix a : 1 2 3 4 5 6 7 8 9 10 11 12 (4x3 matrix) and 1 1 1 2 3 4 (2 x3 matrix) is one matrix so 1 2 3 4 5 6 7 8 9 10 11 12 1 1 2 3 4 (6x3 matrx) @Paper-SSheets – klion Feb 18 '19 at 03:41
  • Ah, so like this? (can't include an image so I guess use this link) https://www.symbolab.com/solver/inequalities-calculator/%5Cbegin%7Bpmatrix%7D1%262%263%5C%5C%20%204%265%266%5C%5C%20%207%268%269%5C%5C%20%2010%2611%2612%5Cend%7Bpmatrix%7D%5Cbegin%7Bpmatrix%7D1%261%261%5C%5C%20%202%263%264%5Cend%7Bpmatrix%7D-%3E%5Cbegin%7Bpmatrix%7D1%262%263%5C%5C%20%204%265%266%5C%5C%20%207%268%269%5C%5C%20%2010%2611%2612%5C%5C%20%201%261%261%5C%5C%20%202%263%264%5Cend%7Bpmatrix%7D – Paper-SSheets Feb 18 '19 at 03:49
  • Yes, exactly like that @Paper-SSheets – klion Feb 18 '19 at 03:52
  • Awesome, give me a few minutes, I'm working on a solution and a test. – Paper-SSheets Feb 18 '19 at 03:54
  • How would I do the same for prepending? @Paper-SSheets – klion Feb 18 '19 at 04:29
  • You will notice that if you simply swap the matrices, it will provide you exactly what you want. If you were to make a prepend method, it would make sense to simply have that method call the other method, and swap the positions. I'll write it in just one second so you can see, as well as with some sample output. – Paper-SSheets Feb 18 '19 at 04:35
  • Let me know if this helped! This is actually my first time answering on SOF, so let me know if anything is unclear! – Paper-SSheets Feb 18 '19 at 04:45
  • Thank this was very clear and helped a lot @Paper-SSheets – klion Feb 18 '19 at 05:08