Lesson 3
Matrix Manipulation with TypeScript: Combining Submatrices
Introduction

Hello, Coder! This unit's exciting programming lesson will involve traversing the labyrinth of two-dimensional matrices. We're going to apply our TypeScript 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.

Task Statement

Are you ready for the task? Here it is: imagine having two different 2D arrays, A and B. Our job is to devise a TypeScript 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]]
Step Overview

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.
Extracting Submatrices

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

TypeScript
1function extractSubmatrix(matrix: number[][], coords: number[]): number[][] { 2 const [startRow, endRow, startCol, endCol] = coords; 3 const numRows = endRow - startRow + 1; 4 const numCols = endCol - startCol + 1; 5 6 let submatrix: number[][] = []; 7 for (let i = 0; i < numRows; i++) { 8 submatrix.push([]); 9 for (let j = 0; j < numCols; j++) { 10 submatrix[i].push(matrix[startRow + i - 1][startCol + j - 1]); 11 } 12 } 13 return submatrix; 14}
Combining Rows

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 TypeScript:

TypeScript
1function submatrixConcatenation(matrixA: number[][], matrixB: number[][], submatrixCoords: number[][]): number[][] { 2 let submatrixA = extractSubmatrix(matrixA, submatrixCoords[0]); 3 let submatrixB = extractSubmatrix(matrixB, submatrixCoords[1]); 4 5 let resultMatrix: number[][] = []; 6 for (let i = 0; i < submatrixA.length; i++) { 7 resultMatrix.push([...submatrixA[i], ...submatrixB[i]]); 8 } 9 10 return resultMatrix; 11} 12 13const matrixA: number[][] = [ 14 [1, 2, 3, 4], 15 [5, 6, 7, 8], 16 [9, 10, 11, 12] 17]; 18 19const matrixB: number[][] = [ 20 [11, 12, 13], 21 [14, 15, 16], 22 [17, 18, 19] 23]; 24 25const submatrixCoords: number[][] = [ 26 [2, 3, 2, 3], 27 [1, 2, 1, 2] 28]; 29 30const result = submatrixConcatenation(matrixA, matrixB, submatrixCoords); 31for (const row of result) { 32 console.log(row.join(" ")); 33} 34 35// Output: 36// 6 7 11 12 37// 10 11 14 15
Lesson Summary

Congratulations! You've tackled an elaborate matrix manipulation task in this unit. This required you to have a clear understanding of TypeScript's 2D arrays, along with its static type system and how it enhances code reliability through type checking. Through this exercise, not only have you honed your TypeScript 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 while leveraging TypeScript's typing, 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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.