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:

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

and the matrix B as:

text
{{11, 12, 13},
 {14, 15, 16},
 {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:

text
{{6, 7, 11, 12},
 {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++
#include <iostream>
#include <vector>

std::vector<std::vector<int>> submatrixConcatenation(
    const std::vector<std::vector<int>>& matrixA, 
    const std::vector<std::vector<int>>& matrixB, 
    std::vector<std::vector<int>> submatrixCoords) 
{
    int startRowA = submatrixCoords[0][0];
    int endRowA = submatrixCoords[0][1];
    int startColA = submatrixCoords[0][2];
    int endColA = submatrixCoords[0][3];
    int startRowB = submatrixCoords[1][0];
    int endRowB = submatrixCoords[1][1];
    int startColB = submatrixCoords[1][2];
    int endColB = submatrixCoords[1][3];

    std::vector<std::vector<int>> submatrixA;
    for (int i = startRowA - 1; i < endRowA; ++i) {
        std::vector<int> rowA;
        for (int j = startColA - 1; j < endColA; ++j) {
            rowA.push_back(matrixA[i][j]);
        }
        submatrixA.push_back(rowA);
    }

    std::vector<std::vector<int>> submatrixB;
    for (int i = startRowB - 1; i < endRowB; ++i) {
        std::vector<int> rowB;
        for (int j = startColB - 1; j < endColB; ++j) {
            rowB.push_back(matrixB[i][j]);
        }
        submatrixB.push_back(rowB);
    }
    
    // At this point, we have extracted submatrices from matrixA and matrixB
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal