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 JavaScript skills to combine submatrices from two different matrices, creating a new one. It might appear to be quite a complex task, but don't fret! We'll go through this together, one step at a time, and by the end of the lesson you'll be a matrix master.

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

and `B`

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

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

and `B`

. This function 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]]`

To tackle this problem, we will take the following steps:

**Extract Submatrices**: Identify and extract the specified submatrices from matrices`A`

and`B`

using the given coordinates.**Combine Rows**: Concatenate the corresponding rows from both submatrices to form a new matrix.**Return Result Matrix**: Compile and return the newly formed matrix from the concatenated rows.

The 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 JavaScript to get the rows and then the required columns from those rows:

JavaScript`1function submatrixConcatenation(matrixA, matrixB, submatrixCoords) { 2 let startRowA = submatrixCoords[0][0]; 3 let endRowA = submatrixCoords[0][1]; 4 let startColA = submatrixCoords[0][2]; 5 let endColA = submatrixCoords[0][3]; 6 let startRowB = submatrixCoords[1][0]; 7 let endRowB = submatrixCoords[1][1]; 8 let startColB = submatrixCoords[1][2]; 9 let endColB = submatrixCoords[1][3]; 10 11 let numRows = endRowA - startRowA + 1; 12 let numColsA = endColA - startColA + 1; 13 let numColsB = endColB - startColB + 1; 14 15 let submatrixA = []; 16 // Loop through the number of rows in the submatrix 17 for (let i = 0; i < numRows; i++) { 18 submatrixA.push([]); 19 // Nested loop through the number of columns in submatrixA 20 for (let j = 0; j < numColsA; j++) { 21 // Extract the element from matrixA and push it into submatrixA 22 submatrixA[i].push(matrixA[startRowA + i - 1][startColA + j - 1]); 23 } 24 } 25 26 let submatrixB = []; 27 // Loop through the number of rows in the submatrix 28 for (let i = 0; i < numRows; i++) { 29 submatrixB.push([]); 30 // Nested loop through the number of columns in submatrixB 31 for (let j = 0; j < numColsB; j++) { 32 // Extract the element from matrixB and push it into submatrixB 33 submatrixB[i].push(matrixB[startRowB + i - 1][startColB + j - 1]); 34 } 35 } 36 37 // At this point, we have extracted submatrices from matrixA and matrixB 38}`

Once we've successfully extracted the required submatrices, the next step is to stitch them together. To achieve this, we will concatenate the corresponding rows from both submatrices into a new matrix. This can be easily done using simple loops in JavaScript:

JavaScript`1function submatrixConcatenation(matrixA, matrixB, submatrixCoords) { 2 let startRowA = submatrixCoords[0][0]; 3 let endRowA = submatrixCoords[0][1]; 4 let startColA = submatrixCoords[0][2]; 5 let endColA = submatrixCoords[0][3]; 6 let startRowB = submatrixCoords[1][0]; 7 let endRowB = submatrixCoords[1][1]; 8 let startColB = submatrixCoords[1][2]; 9 let endColB = submatrixCoords[1][3]; 10 11 let numRows = endRowA - startRowA + 1; 12 let numColsA = endColA - startColA + 1; 13 let numColsB = endColB - startColB + 1; 14 15 let submatrixA = []; 16 for (let i = 0; i < numRows; i++) { // Outer loop to iterate through rows of submatrix A 17 submatrixA.push([]); 18 for (let j = 0; j < numColsA; j++) { // Inner loop to iterate through columns of submatrix A 19 submatrixA[i].push(matrixA[startRowA + i - 1][startColA + j - 1]); 20 } 21 } 22 23 let submatrixB = []; 24 for (let i = 0; i < numRows; i++) { // Outer loop to iterate through rows of submatrix B 25 submatrixB.push([]); 26 for (let j = 0; j < numColsB; j++) { // Inner loop to iterate through columns of submatrix B 27 submatrixB[i].push(matrixB[startRowB + i - 1][startColB + j - 1]); 28 } 29 } 30 31 // The part for the concatenation process 32 let resultMatrix = []; 33 for (let i = 0; i < numRows; i++) { // Outer loop to iterate through rows of the result matrix 34 resultMatrix.push([]); 35 for (let j = 0; j < numColsA; j++) { // Inner loop to add elements from submatrix A to the result matrix row 36 resultMatrix[i].push(submatrixA[i][j]); 37 } 38 for (let j = 0; j < numColsB; j++) { // Inner loop to add elements from submatrix B to the result matrix row 39 resultMatrix[i].push(submatrixB[i][j]); 40 } 41 } 42 43 return resultMatrix; 44} 45 46let matrixA = [ 47 [1, 2, 3, 4], 48 [5, 6, 7, 8], 49 [9, 10, 11, 12] 50]; 51 52let matrixB = [ 53 [11, 12, 13], 54 [14, 15, 16], 55 [17, 18, 19] 56]; 57 58let submatrixCoords = [ 59 [2, 3, 2, 3], 60 [1, 2, 1, 2] 61]; 62 63let result = submatrixConcatenation(matrixA, matrixB, submatrixCoords); 64for (let row of result) { 65 console.log(row.join(" ")); 66}`

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

Now that you've grasped the process of extracting submatrices and combining them, you're more equipped to handle similar challenges. This lesson is your foundation, and with further practice, you'll become adept at solving complex matrix manipulation tasks. Happy coding!