Introduction

Hello Coders! This unit's exciting programming lesson will involve traversing the labyrinth of two-dimensional matrices. We're going to apply our Python 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 Python function—let's name it submatrix_concatenation()—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

[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]]

and the matrix B as

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

[[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 Python's list slicing technique to get the rows, and then the required columns from those rows:

def submatrix_concatenation(matrix_A, matrix_B, submatrix_coords):
    start_row_A, end_row_A, start_col_A, end_col_A = submatrix_coords[0]
    start_row_B, end_row_B, start_col_B, end_col_B = submatrix_coords[1]

    # Using list slicing to extract the required rows and columns
    submatrix_A = [row[start_col_A-1:end_col_A] for row in matrix_A[start_row_A-1:end_row_A]]
    submatrix_B = [row[start_col_B-1:end_col_B] for row in matrix_B[start_row_B-1:end_row_B]]
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. It's easy to accomplish this by using Python's list concatenation (+ operator) and list comprehension:

def submatrix_concatenation(matrix_A, matrix_B, submatrix_coords):
    start_row_A, end_row_A, start_col_A, end_col_A = submatrix_coords[0]
    start_row_B, end_row_B, start_col_B, end_col_B = submatrix_coords[1]

    # Using list slicing to extract the required rows and columns
    submatrix_A = [row[start_col_A-1:end_col_A] for row in matrix_A[start_row_A-1:end_row_A]]
    submatrix_B = [row[start_col_B-1:end_col_B] for row in matrix_B[start_row_B-1:end_row_B]]

    # Using list concatenation and list comprehension to combine the submatrices
    result_matrix = [row_A + row_B for row_A, row_B in zip(submatrix_A, submatrix_B)]
    return result_matrix

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

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