Lesson 3
Submatrix Concatenation in C++
Introduction

Hello Coders! 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 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.

Task Statement

Are you ready for the task? Here it is: Imagine having two different 2D matrices, 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 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}}
Solution Building: Step 1

Our first step towards the solution is to extract submatrices from A and B from 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++
1#include <iostream> 2#include <vector> 3 4std::vector<std::vector<int>> submatrixConcatenation( 5 const std::vector<std::vector<int>>& matrixA, 6 const std::vector<std::vector<int>>& matrixB, 7 std::vector<std::vector<int>> submatrixCoords) 8{ 9 int startRowA = submatrixCoords[0][0]; 10 int endRowA = submatrixCoords[0][1]; 11 int startColA = submatrixCoords[0][2]; 12 int endColA = submatrixCoords[0][3]; 13 int startRowB = submatrixCoords[1][0]; 14 int endRowB = submatrixCoords[1][1]; 15 int startColB = submatrixCoords[1][2]; 16 int endColB = submatrixCoords[1][3]; 17 18 std::vector<std::vector<int>> submatrixA; 19 for (int i = startRowA - 1; i < endRowA; ++i) { 20 std::vector<int> rowA; 21 for (int j = startColA - 1; j < endColA; ++j) { 22 rowA.push_back(matrixA[i][j]); 23 } 24 submatrixA.push_back(rowA); 25 } 26 27 std::vector<std::vector<int>> submatrixB; 28 for (int i = startRowB - 1; i < endRowB; ++i) { 29 std::vector<int> rowB; 30 for (int j = startColB - 1; j < endColB; ++j) { 31 rowB.push_back(matrixB[i][j]); 32 } 33 submatrixB.push_back(rowB); 34 } 35 36 // At this point, we have extracted submatrices from matrixA and matrixB
Solution Building: Step 2

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 C++ vector operations and loops:

C++
1vector<std::vector<int>> submatrixConcatenation( 2 const std::vector<std::vector<int>>& matrixA, 3 const std::vector<std::vector<int>>& matrixB, 4 std::vector<std::vector<int>> submatrixCoords) 5{ 6 int startRowA = submatrixCoords[0][0]; 7 int endRowA = submatrixCoords[0][1]; 8 int startColA = submatrixCoords[0][2]; 9 int endColA = submatrixCoords[0][3]; 10 int startRowB = submatrixCoords[1][0]; 11 int endRowB = submatrixCoords[1][1]; 12 int startColB = submatrixCoords[1][2]; 13 int endColB = submatrixCoords[1][3]; 14 15 std::vector<std::vector<int>> submatrixA; 16 for (int i = startRowA - 1; i < endRowA; ++i) { 17 std::vector<int> rowA; 18 for (int j = startColA - 1; j < endColA; ++j) { 19 rowA.push_back(matrixA[i][j]); 20 } 21 submatrixA.push_back(rowA); 22 } 23 24 std::vector<std::vector<int>> submatrixB; 25 for (int i = startRowB - 1; i < endRowB; ++i) { 26 std::vector<int> rowB; 27 for (int j = startColB - 1; j < endColB; ++j) { 28 rowB.push_back(matrixB[i][j]); 29 } 30 submatrixB.push_back(rowB); 31 } 32 33 std::vector<std::vector<int>> resultMatrix; 34 for (size_t i = 0; i < submatrixA.size(); ++i) { 35 std::vector<int> resultRow = submatrixA[i]; 36 resultRow.insert(resultRow.end(), submatrixB[i].begin(), submatrixB[i].end()); 37 resultMatrix.push_back(resultRow); 38 } 39 40 return resultMatrix; 41}

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

Lesson Summary

Congratulations! You've tackled an elaborate matrix manipulation task in this unit. This required you to have a clear understanding of C++ vectors 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, 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!

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