Lesson 3
Matrix Submatrix Concatenation in C#
Introduction

Hello, Coder! This unit's exciting programming lesson will involve traversing the labyrinth of two-dimensional matrices. We're going to apply our C# 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 C# 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-3rd rows and 2nd-3rd columns from A, and 1st-2nd rows and 1st-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 using the given coordinates. For this, we'll use nested loops in C# to get the rows and then the required columns from those rows:

C#
1using System; 2using System.Collections.Generic; 3 4class Program 5{ 6 static int[,] SubmatrixConcatenation(int[,] matrixA, int[,] matrixB, int[][] submatrixCoords) 7 { 8 int startRowA = submatrixCoords[0][0] - 1; 9 int endRowA = submatrixCoords[0][1] - 1; 10 int startColA = submatrixCoords[0][2] - 1; 11 int endColA = submatrixCoords[0][3] - 1; 12 int startRowB = submatrixCoords[1][0] - 1; 13 int endRowB = submatrixCoords[1][1] - 1; 14 int startColB = submatrixCoords[1][2] - 1; 15 int endColB = submatrixCoords[1][3] - 1; 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 { 24 for (int j = 0; j < numColsA; j++) 25 { 26 submatrixA[i, j] = matrixA[startRowA + i, startColA + j]; 27 } 28 } 29 30 int[,] submatrixB = new int[numRows, numColsB]; 31 for (int i = 0; i < numRows; i++) 32 { 33 for (int j = 0; j < numColsB; j++) 34 { 35 submatrixB[i, j] = matrixB[startRowB + i, startColB + j]; 36 } 37 } 38 39 return CombineSubmatrices(numRows, numColsA, numColsB, submatrixA, submatrixB); 40 }
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 C#, as seen in the code below. Finally, we add two functions: one for printing the matrix and another for the main program execution.

C#
1 static int[,] CombineSubmatrices(int numRows, int numColsA, int numColsB, int[,] submatrixA, int[,] submatrixB) 2 { 3 int[,] resultMatrix = new int[numRows, numColsA + numColsB]; 4 for (int i = 0; i < numRows; i++) 5 { 6 for (int j = 0; j < numColsA; j++) 7 { 8 resultMatrix[i, j] = submatrixA[i, j]; 9 } 10 for (int j = 0; j < numColsB; j++) 11 { 12 resultMatrix[i, j + numColsA] = submatrixB[i, j]; 13 } 14 } 15 16 return resultMatrix; 17 } 18 19 static void PrintMatrix(int[,] matrix) 20 { 21 int rows = matrix.GetLength(0); 22 int cols = matrix.GetLength(1); 23 for (int i = 0; i < rows; i++) 24 { 25 for (int j = 0; j < cols; j++) 26 { 27 Console.Write(matrix[i, j] + " "); 28 } 29 Console.WriteLine(); 30 } 31 } 32 33 static void Main() 34 { 35 int[,] matrixA = { 36 {1, 2, 3, 4}, 37 {5, 6, 7, 8}, 38 {9, 10, 11, 12} 39 }; 40 41 int[,] matrixB = { 42 {11, 12, 13}, 43 {14, 15, 16}, 44 {17, 18, 19} 45 }; 46 47 int[][] submatrixCoords = new int[][] { 48 new int[] {2, 3, 2, 3}, 49 new int[] {1, 2, 1, 2} 50 }; 51 52 int[,] result = SubmatrixConcatenation(matrixA, matrixB, submatrixCoords); 53 PrintMatrix(result); 54 } 55}
Lesson Summary

Congratulations! You've tackled an elaborate matrix manipulation task in this unit. This required you to have a clear understanding of C#'s 2D arrays and nested loops. Through this exercise, not only have you honed your C# 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!

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