Program to concatenate two given Matrices of same size
Last Updated :
07 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
#define M 2
#define N 2
void mergeMatrix( int A[M][N],
int B[M][N])
{
int C[M][2 * N];
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
C[i][j] = A[i][j];
C[i][j + N] = B[i][j];
}
}
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < 2 * N;
j++)
cout << C[i][j] << " " ;
cout << endl;
}
}
int main()
{
int A[M][N] = { { 1, 2 },
{ 3, 4 } };
int B[M][N] = { { 5, 6 },
{ 7, 8 } };
mergeMatrix(A, B);
return 0;
}
|
Java
class GFG{
static final int M = 2 ;
static final int N = 2 ;
static void mergeMatrix( int A[][],
int B[][])
{
int [][]C = new int [M][ 2 * N];
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < N; j++)
{
C[i][j] = A[i][j];
C[i][j + N] = B[i][j];
}
}
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < 2 * N; j++)
System.out.print(C[i][j] + " " );
System.out.println();
}
}
public static void main(String[] args)
{
int A[][] = { { 1 , 2 },
{ 3 , 4 } };
int B[][] = { { 5 , 6 },
{ 7 , 8 } };
mergeMatrix(A, B);
}
}
|
Python3
M = 2
N = 2
def mergeMatrix(A, B):
C = [[ 0 for j in range ( 2 * N)]
for i in range (M)]
for i in range (M):
for j in range (N):
C[i][j] = A[i][j]
C[i][j + N] = B[i][j]
for i in range (M):
for j in range ( 2 * N):
print (C[i][j], end = ' ' )
print ()
if __name__ = = '__main__' :
A = [ [ 1 , 2 ], [ 3 , 4 ] ]
B = [ [ 5 , 6 ], [ 7 , 8 ] ]
mergeMatrix(A, B)
|
C#
using System;
class GFG{
const int M = 2;
const int N = 2;
static void mergeMatrix( int [,] A, int [,] B)
{
int [,] C = new int [M, 2 * N];
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
C[i, j] = A[i, j];
C[i, j + N] = B[i, j];
}
}
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < 2 * N; j++)
Console.Write(C[i, j] + " " );
Console.WriteLine();
}
}
static void Main()
{
int [,] A = { { 1, 2 },
{ 3, 4 } };
int [,] B = { { 5, 6 },
{ 7, 8 } };
mergeMatrix(A, B);
}
}
|
Javascript
<script>
M = 2
N = 2
function mergeMatrix(A,B)
{
var C = [[0,0,0,0], [0,0,0,0]]
for ( var i = 0; i < M; i++)
{
for ( var j = 0; j < N; j++)
{
C[i][j] = A[i][j];
C[i][j + N] = B[i][j];
}
}
for ( var i = 0; i < M; i++)
{
for ( var j = 0; j < 2 * N;
j++)
document.write( C[i][j] + " " );
document.write( "<br>" );
}
}
var A = [ [ 1, 2 ],
[ 3, 4 ] ];
var B = [ [ 5, 6 ],
[ 7, 8 ] ];
mergeMatrix(A, B);
</script>
|
Time Complexity: O(M * N)
Auxiliary Space: O(M * N), since M * N extra space has been taken.
Share your thoughts in the comments
Please Login to comment...