Program to concatenate two given Matrices of same size

12 Apr, 2021
Given two matrices A and B of size M x N, the task is to concatenate them into one matrix.Concatenation of matrix: The process of appending elements of every row of the matrix one after the other is known as the Concatenation of matrix. Examples: 
Input:
A[][] = {{1, 2}, 
         {3, 4}},
B[][] = {{5, 6}, 
         {7, 8}}
Output: 
1 2 5 6
3 4 7 8
Explanation:
Elements of every row of the matrix B 
is appended into the row of matrix A.

Input: 
A[][] = {{2, 4}, 
         {3, 4}}
B[][] = {{1, 2}, 
         {1, 3}}       
Output: 
2 4 1 2 
3 4 1 3
Approach: The idea is to declare a matrix of size N x 2*M, then traverse over matrix A and store the elements in the first half of the matrix and then similarly iterate over matrix B and store the elements in another half of the matrix with the help of the below formulae: 
// For filling the
// first half of matrix
matrix[i][j] = A[i][j]

// For filling the
// second half of matrix
matrix[i][j+N] = A[i][j + N]
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation to concatenate// two matrices of size N x M #include <bits/stdc++.h> using namespace std; #define M 2#define N 2 // Function to concatenate two// matrices A[][] and B[][]void mergeMatrix(int A[M][N],                 int B[M][N]){     // Matrix to store    // the result    int C[M][2 * N];     // Merge the two matrices    for (int i = 0; i < M; i++) {        for (int j = 0; j < N; j++) {             // To store elements            // of matrix A            C[i][j] = A[i][j];             // To store elements            // of matrix B            C[i][j + N] = B[i][j];        }    }     // Print the result    for (int i = 0; i < M; i++) {        for (int j = 0; j < 2 * N;             j++)            cout << C[i][j] << " ";        cout << endl;    }} // Driven Codeint main(){    int A[M][N] = { { 1, 2 },                    { 3, 4 } };     int B[M][N] = { { 5, 6 },                    { 7, 8 } };     // Find the merge of    // the 2 matrices    mergeMatrix(A, B);     return 0;}
// Java implementation to concatenate// two matrices of size N x Mclass GFG{ static final int M = 2;static final int N = 2; // Function to concatenate two// matrices A[][] and B[][]static void mergeMatrix(int A[][],                        int B[][]){     // Matrix to store    // the result    int [][]C = new int[M][2 * N];     // Merge the two matrices    for(int i = 0; i < M; i++)    {       for(int j = 0; j < N; j++)       {          // To store elements          // of matrix A          C[i][j] = A[i][j];                     // To store elements          // of matrix B          C[i][j + N] = B[i][j];                   }    }     // Print the result    for(int i = 0; i < M; i++)    {       for(int j = 0; j < 2 * N; j++)          System.out.print(C[i][j] + " ");        System.out.println();    }} // Driven Codepublic static void main(String[] args){    int A[][] = { { 1, 2 },                  { 3, 4 } };     int B[][] = { { 5, 6 },                  { 7, 8 } };     // Find the merge of    // the 2 matrices    mergeMatrix(A, B);}} // This code is contributed by gauravrajput1
# Python3 implementation to concatenate# two matrices of size N x M M = 2N = 2 # Function to concatenate two# matrices A[][] and B[][]def mergeMatrix(A, B):     # Matrix to store    # the result    C = [[0 for j in range(2 * N)]            for i in range(M)]         # Merge the two matrices    for i in range(M):        for j in range(N):                         # To store elements            # of matrix A            C[i][j] = A[i][j]                         # To store elements            # of matrix B            C[i][j + N] = B[i][j]         # Print the result    for i in range(M):        for j in range(2 * N):                print(C[i][j], end = ' ')                     print() # Driver codeif __name__=='__main__':         A = [ [1, 2], [3, 4] ]    B = [ [5, 6], [7, 8] ]         # Find the merge of    # the 2 matrices    mergeMatrix(A, B) # This code is contributed by rutvik_56
// C# implementation to concatenate// two matrices of size N x Musing System; class GFG{     const int M = 2;const int N = 2;     // Function to concatenate two// matrices A[][] and B[][]static void mergeMatrix(int[,] A, int[,] B){         // Matrix to store    // the result    int[,] C = new int[M, 2 * N];         // Merge the two matrices    for(int i = 0; i < M; i++)    {       for(int j = 0; j < N; j++)       {                      // To store elements          // of matrix A          C[i, j] = A[i, j];                     // To store elements          // of matrix B          C[i, j + N] = B[i, j];       }    }         // Print the result    for(int i = 0; i < M; i++)    {       for(int j = 0; j < 2 * N; j++)          Console.Write(C[i, j] + " ");            Console.WriteLine();    }} // Driver codestatic void Main(){    int[,] A = { { 1, 2 },                 { 3, 4 } };     int[,] B = { { 5, 6 },                 { 7, 8 } };         // Find the merge of    // the 2 matrices    mergeMatrix(A, B);}} // This code is contributed by divyeshrabadiya07
<script>// Javascript implementation to concatenate// two matrices of size N x MM = 2N = 2 // Function to concatenate two// matrices A[][] and B[][]function mergeMatrix(A,B){     // Matrix to store    // the result    var C = [[0,0,0,0], [0,0,0,0]]     // Merge the two matrices    for (var i = 0; i < M; i++)    {        for (var j = 0; j < N; j++)        {             // To store elements            // of matrix A            C[i][j] = A[i][j];             // To store elements            // of matrix B            C[i][j + N] = B[i][j];        }    }     // Print the result    for (var i = 0; i < M; i++)    {        for (var j = 0; j < 2 * N;             j++)            document.write( C[i][j] + " ");        document.write("<br>");    }} // Driven Codevar A = [ [ 1, 2 ],                [ 3, 4 ] ];var B = [ [ 5, 6 ],                [ 7, 8 ] ];                 // Find the merge of// the 2 matricesmergeMatrix(A, B); // This code is contributed by noob2000.</script>
1 2 5 6 
3 4 7 8
 
Time Complexity: O(M * N)
Auxiliary Space: O(M * N)
GauravRajput1
rutvik_56
divyeshrabadiya07
subhammahato348
noob2000
Arrays
Matrix
School Programming
Arrays
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Search, insert and delete in an unsorted array
Window Sliding Technique
Find duplicates in O(n) time and O(1) extra space | Set 1
Chocolate Distribution Problem
Matrix Chain Multiplication | DP-8
Rat in a Maze | Backtracking-2
Sudoku | Backtracking-7
The Celebrity Problem
Find the number of islands | Set 1 (Using DFS)