Lesson 3

Hello Coder! This unit's exciting programming lesson will involve traversing the labyrinth of two-dimensional matrices. We're going to apply our Java skills to combine submatrices from two different matrices, creating a new one. It appears to be quite a complex task, doesn't it? But don't fret. We'll go through this together, one step at a time.

Are you ready for the task? Here it is: Imagine having two different 2D arrays, `A`

and `B`

. Our job is to devise a Java method — let's name it `submatrixConcatenation()`

— which takes these two matrices as inputs, along with the coordinates specifying submatrices within `A`

and `B`

. This method is expected to stitch the two chosen submatrices together, forming a new one, `C`

. Notably, the submatrices from `A`

and `B`

should have the same number of rows, and in the final matrix `C`

, elements from `A`

's submatrix should be on the left and those from `B`

's submatrix on the right.

Let's visualize this with a couple of matrices.

Given the matrix `A`

as:

Plain text`1{{1, 2, 3, 4}, 2 {5, 6, 7, 8}, 3 {9, 10, 11, 12}}`

and the matrix `B`

as:

Plain text`1{{11, 12, 13}, 2 {14, 15, 16}, 3 {17, 18, 19}}`

If we select 2x2 submatrices from each (comprising the 2nd to 3rd rows and 2nd to 3rd columns from `A`

, and 1st to 2nd rows and 1st to 2nd columns from `B`

), their concatenation would look like:

Plain text`1{{6, 7, 11, 12}, 2 {10, 11, 14, 15}}`

Our first step toward the solution is to extract submatrices from `A`

and `B`

from the given coordinates. For this, we'll use nested loops in Java to get the rows and then the required columns from those rows:

Java`1class Solution { 2 3 public static int[][] submatrixConcatenation( 4 int[][] matrixA, 5 int[][] matrixB, 6 int[][] submatrixCoords) { 7 8 int startRowA = submatrixCoords[0][0]; 9 int endRowA = submatrixCoords[0][1]; 10 int startColA = submatrixCoords[0][2]; 11 int endColA = submatrixCoords[0][3]; 12 int startRowB = submatrixCoords[1][0]; 13 int endRowB = submatrixCoords[1][1]; 14 int startColB = submatrixCoords[1][2]; 15 int endColB = submatrixCoords[1][3]; 16 17 int numRows = endRowA - startRowA + 1; 18 int numColsA = endColA - startColA + 1; 19 int numColsB = endColB - startColB + 1; 20 21 int[][] submatrixA = new int[numRows][numColsA]; 22 for (int i = 0; i < numRows; i++) { 23 for (int j = 0; j < numColsA; j++) { 24 submatrixA[i][j] = matrixA[startRowA + i - 1][startColA + j - 1]; 25 } 26 } 27 28 int[][] submatrixB = new int[numRows][numColsB]; 29 for (int i = 0; i < numRows; i++) { 30 for (int j = 0; j < numColsB; j++) { 31 submatrixB[i][j] = matrixB[startRowB + i - 1][startColB + j - 1]; 32 } 33 } 34 35 // At this point, we have extracted submatrices from matrixA and matrixB 36 } 37}`

Next, we need to stitch together these submatrices. We can achieve this by concatenating corresponding rows from both matrices into a new matrix. This can be done using loops in Java:

Java`1class Solution { 2 3 public static int[][] submatrixConcatenation( 4 int[][] matrixA, 5 int[][] matrixB, 6 int[][] submatrixCoords) { 7 8 int startRowA = submatrixCoords[0][0]; 9 int endRowA = submatrixCoords[0][1]; 10 int startColA = submatrixCoords[0][2]; 11 int endColA = submatrixCoords[0][3]; 12 int startRowB = submatrixCoords[1][0]; 13 int endRowB = submatrixCoords[1][1]; 14 int startColB = submatrixCoords[1][2]; 15 int endColB = submatrixCoords[1][3]; 16 17 int numRows = endRowA - startRowA + 1; 18 int numColsA = endColA - startColA + 1; 19 int numColsB = endColB - startColB + 1; 20 21 int[][] submatrixA = new int[numRows][numColsA]; 22 for (int i = 0; i < numRows; i++) { 23 for (int j = 0; j < numColsA; j++) { 24 submatrixA[i][j] = matrixA[startRowA + i - 1][startColA + j - 1]; 25 } 26 } 27 28 int[][] submatrixB = new int[numRows][numColsB]; 29 for (int i = 0; i < numRows; i++) { 30 for (int j = 0; j < numColsB; j++) { 31 submatrixB[i][j] = matrixB[startRowB + i - 1][startColB + j - 1]; 32 } 33 } 34 35 // The part for the concatenation process 36 int[][] resultMatrix = new int[numRows][numColsA + numColsB]; 37 for (int i = 0; i < numRows; i++) { 38 for (int j = 0; j < numColsA; j++) { 39 resultMatrix[i][j] = submatrixA[i][j]; 40 } 41 for (int j = 0; j < numColsB; j++) { 42 resultMatrix[i][j + numColsA] = submatrixB[i][j]; 43 } 44 } 45 46 return resultMatrix; 47 } 48 49 public static void main(String[] args) { 50 int[][] matrixA = { 51 {1, 2, 3, 4}, 52 {5, 6, 7, 8}, 53 {9, 10, 11, 12} 54 }; 55 56 int[][] matrixB = { 57 {11, 12, 13}, 58 {14, 15, 16}, 59 {17, 18, 19} 60 }; 61 62 int[][] submatrixCoords = { 63 {2, 3, 2, 3}, 64 {1, 2, 1, 2} 65 }; 66 67 int[][] result = submatrixConcatenation(matrixA, matrixB, submatrixCoords); 68 for (int[] row : result) { 69 for (int val : row) { 70 System.out.print(val + " "); 71 } 72 System.out.println(); 73 } 74 } 75}`

There we go! We've combined submatrices from two matrices into one, provided they have the same number of rows.

Congratulations! You've tackled an elaborate matrix manipulation task in this unit. This required you to have a clear understanding of Java's 2D arrays and nested loops. Through this exercise, not only have you honed your Java proficiency, but you've also wrestled with the conceptual intricacies of submatrices.

Now, it's time for some hands-on practice! In the subsequent practice session, dive into more complex challenges that involve manipulating and working with multiple matrices. This lesson will be your handy guide as you explore similar problems. Keep practicing, and soon you'll be adept at solving matrix manipulation tasks. Happy coding!