The transpose of a matrix is obtained by interchanging the rows and columns of the original matrix. In this article, you'll learn how to find the transpose of a square and rectangular matrix using C++, Python, JavaScript, and C.

Problem Statement

You're given a matrix mat[][]. You need to find and print the transpose of the matrix.

Examples:

Transpose of a matrix

How to Find the Transpose of a Rectangular Matrix

  1. The order of the transpose of a rectangular matrix is opposite to that of the original matrix. For example, if the order of the original matrix is 3 x 4, therefore the order of the transpose of this matrix would be 4 x 3.
  2. Store every column of the original matrix as rows in the transposed matrix i.e., transposeMatrix[i][j] = mat[j][i].

C++ Program to Find the Transpose of a Rectangular Matrix

Below is the C++ program to find the transpose of a rectangular matrix:

        // C++ program to find the transpose of a rectangular Matrix
#include <iostream>
using namespace std;

// The order of the initial matrix is 3 x 4
#define size1 3
#define size2 4

// Function to transpose a Matrix
void transposeMatrix(int mat[][size2], int transposeMatrix[][size1])
{
 for (int i=0; i<size2; i++)
 {
 for (int j=0; j<size1; j++)
 {
 transposeMatrix[i][j] = mat[j][i];
 }
 }
}

// Driver Code
int main()
{
 int mat[size1][size2] = { {4, 2, 8, 2},
 {9, 7, 1, 9},
 {0, 2, 7, 5} };

cout << "Initial Matrix:" << endl;

// Printing the initial Matrix
 for (int i = 0; i < size1; i++)
 {
 for (int j = 0; j < size2; j++)
 {
 cout << mat[i][j] << " ";
 }
 cout << endl;
 }

// Variable to store the transposed Matrix
 // The dimensions of transposedMatrix are opposite to that of mat
 int transposedMatrix[size2][size1];

transposeMatrix(mat, transposedMatrix);

cout << "Transposed Matrix:" << endl;

// Printing the transposed Matrix
 for (int i = 0; i < size2; i++)
 {
 for (int j = 0; j < size1; j++)
 {
 cout << transposedMatrix[i][j] << " ";
 }
 cout << endl;
 }

return 0;
}

Output:

        Initial Matrix:
4 2 8 2
9 7 1 9
0 2 7 5
Transposed Matrix:
4 9 0
2 7 2
8 1 7
2 9 5

Python Program to Find the Transpose of a Rectangular Matrix

Below is the Python program to find the transpose of a rectangular matrix:

        # Python program to find the transpose of a rectangular Matrix

# The order of the initial matrix is 3 x 4
size1 = 3
size2 = 4

# Function to transpose a Matrix
def transposeMatrix(mat, transposedMatrix):
for i in range(size2):
for j in range(size1):
transposedMatrix[i][j] = mat[j][i]

# Driver Code
mat = [ [4, 2, 8, 2],
        [9, 7, 1, 9],
        [0, 2, 7, 5] ]

print("Initial Matrix:")

# Printing the initial Matrix
for i in range(size1):
for j in range(size2):
print(mat[i][j], end=' ')
print()

# Variable to store the transposed Matrix
# The dimensions of transposedMatrix are opposite to that of mat
transposedMatrix = [[0 for x in range(size1)] for y in range(size2)]

transposeMatrix(mat, transposedMatrix)

print("Transposed Matrix:")

# Printing the transposed Matrix
for i in range(size2):
for j in range(size1):
print(transposedMatrix[i][j], end=' ')
print()

Output:

        Initial Matrix:
4 2 8 2
9 7 1 9
0 2 7 5
Transposed Matrix:
4 9 0
2 7 2
8 1 7
2 9 5

Related: How to Find the LCM and GCD of Two Numbers in Multiple Languages

JavaScript Program to Find the Transpose of a Rectangular Matrix

Below is the JavaScript program to find the transpose of a rectangular matrix:

        // JavaScript program to find the transpose of a rectangular Matrix

// The order of the initial matrix is 3 x 4
var size1 = 3
var size2 = 4

// Function to transpose a Matrix
function transposeMatrix(mat, transposeMatrix) {
 for (let i=0; i<size2; i++) {
 for (let j=0; j<size1; j++) {
 transposeMatrix[i][j] = mat[j][i];
 }
 }
}

// Driver Code
var mat = [ [4, 2, 8, 2],
 [9, 7, 1, 9],
 [0, 2, 7, 5] ];

document.write("Initial Matrix:" + "
");

// Printing the initial Matrix
for (let i = 0; i < size1; i++) {
 for (let j = 0; j < size2; j++) {
 document.write(mat[i][j] + " ");
 }
 document.write("
");
}

// Variable to store the transposed Matrix
// The dimensions of transposedMatrix are opposite to that of mat1
var transposedMatrix = new Array(size2);

for (let k = 0; k < size2; k++) {
 transposedMatrix[k] = new Array(size1);
}

transposeMatrix(mat, transposedMatrix);

document.write("Transposed Matrix:" + "
");

// Printing the transposed Matrix
for (let i = 0; i < size2; i++) {
 for (let j = 0; j < size1; j++) {
 document.write(transposedMatrix[i][j] + " ");
 }
 document.write("
");
}

Output:

        Initial Matrix:
4 2 8 2
9 7 1 9
0 2 7 5
Transposed Matrix:
4 9 0
2 7 2
8 1 7
2 9 5

Related: How to Swap Two Variables in C++, Python, and JavaScript

C Program to Find the Transpose of a Rectangular Matrix

Below is the C program to find the transpose of a rectangular matrix:

        // C program to find the transpose of a rectangular Matrix
#include <stdio.h>

// The order of the initial matrix is 3 x 4
#define size1 3
#define size2 4

// Function to transpose a Matrix
void transposeMatrix(int mat[][size2], int transposeMatrix[][size1])
{
 for (int i=0; i<size2; i++)
 {
 for (int j=0; j<size1; j++)
 {
 transposeMatrix[i][j] = mat[j][i];
 }
 }
}

// Driver Code
int main()
{
 int mat[size1][size2] = { {4, 2, 8, 2},
 {9, 7, 1, 9},
 {0, 2, 7, 5} };

printf("Initial Matrix: \⁠n");

// Printing the initial Matrix
 for (int i = 0; i < size1; i++)
 {
 for (int j = 0; j < size2; j++)
 {
 printf("%d ", mat[i][j]);
 }
 printf("\⁠n");
 }

// Variable to store the transposed Matrix
 // The dimensions of transposedMatrix are opposite to that of mat1
 int transposedMatrix[size2][size1];

transposeMatrix(mat, transposedMatrix);

printf("Transposed Matrix: \⁠n");

// Printing the transposed Matrix
 for (int i = 0; i < size2; i++)
 {
 for (int j = 0; j < size1; j++)
 {
 printf("%d ", transposedMatrix[i][j]);
 }
 printf("\⁠n");
 }

return 0;
}

Output:

        Initial Matrix:
4 2 8 2
9 7 1 9
0 2 7 5
Transposed Matrix:
4 9 0
2 7 2
8 1 7
2 9 5

How to Find the Transpose of a Square Matrix

  1. The order of transpose of a square matrix is the same as that of the original matrix. For example, if the order of the original matrix is 3 x 3, the order of the transpose of this matrix would still be 3 x 3. Thus, declare a matrix with the same order as that of the original matrix.
  2. Store every column of the original matrix as rows in the transposed matrix i.e., transposeMatrix[i][j] = mat[j][i].

C++ Program to Find the Transpose of a Square Matrix

Below is the C++ program to find the transpose of a square matrix:

        // C++ program to find the transpose of a square matrix
#include <iostream>
using namespace std;

// The order of the matrix is 3 x 3
#define size 3

// Function to transpose a Matrix
void transposeMatrix(int mat[][size], int transposeMatrix[][size])
{
 for (int i=0; i<size; i++)
 {
 for (int j=0; j<size; j++)
 {
 transposeMatrix[i][j] = mat[j][i];
 }
 }
}


int main()
{
 int mat[size][size] = { {4, 2, 8},
 {9, 7, 1},
 {0, 2, 7} };

cout << "Initial Matrix:" << endl;

// Printing the initial Matrix
 for (int i = 0; i < size; i++)
 {
 for (int j = 0; j < size; j++)
 {
 cout << mat[i][j] << " ";
 }
 cout << endl;
 }

// Variable to store the transposed Matrix
 int transposedMatrix[size][size];

transposeMatrix(mat, transposedMatrix);

cout << "Transposed Matrix:" << endl;

// Printing the transposed Matrix
 for (int i = 0; i < size; i++)
 {
 for (int j = 0; j < size; j++)
 {
 cout << transposedMatrix[i][j] << " ";
 }
 cout << endl;
 }

return 0;
}

Output:

        Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7

Python Program to Find the Transpose of a Square Matrix

Below is the Python program to find the transpose of a square matrix:

        # Python program to find the transpose of a square Matrix

# The order of the initial matrix is 3 x 3
size = 3

# Function to transpose a Matrix
def transposeMatrix(mat, transposedMatrix):
for i in range(size):
for j in range(size):
transposedMatrix[i][j] = mat[j][i]

# Driver Code
mat = [ [4, 2, 8],
        [9, 7, 1],
        [0, 2, 7] ]

print("Initial Matrix:")

# Printing the initial Matrix
for i in range(size):
for j in range(size):
print(mat[i][j], end=' ')
print()

# Variable to store the transposed Matrix
transposedMatrix = [[0 for x in range(size)] for y in range(size)]

transposeMatrix(mat, transposedMatrix)

print("Transposed Matrix:")

# Printing the transposed Matrix
for i in range(size):
for j in range(size):
print(transposedMatrix[i][j], end=' ')
print()

Output:

        Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7

Related: How to Find the Sum of Natural Numbers Using Recursion

JavaScript Program to Find the Transpose of a Square Matrix

Below is the JavaScript program to find the transpose of a square matrix:

        // JavaScript program to find the transpose of a square Matrix

// The order of the initial matrix is 3 x 3
var size = 3

// Function to transpose a Matrix
function transposeMatrix(mat, transposeMatrix) {
 for (let i=0; i<size; i++) {
 for (let j=0; j<size; j++) {
 transposeMatrix[i][j] = mat[j][i];
 }
 }
}

// Driver Code
var mat = [ [4, 2, 8],
 [9, 7, 1],
 [0, 2, 7] ];

document.write("Initial Matrix:" + "
");

// Printing the initial Matrix
for (let i = 0; i < size; i++) {
 for (let j = 0; j < size; j++) {
 document.write(mat[i][j] + " ");
 }
 document.write("
");
}

// Variable to store the transposed Matrix
// The dimensions of transposedMatrix are opposite to that of mat1
var transposedMatrix = new Array(size);

for (let k = 0; k < size; k++) {
 transposedMatrix[k] = new Array(size);
}

transposeMatrix(mat, transposedMatrix);

document.write("Transposed Matrix:" + "
");

// Printing the transposed Matrix
for (let i = 0; i < size; i++) {
 for (let j = 0; j < size; j++) {
 document.write(transposedMatrix[i][j] + " ");
 }
 document.write("
");
}

Output:

        Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7

C Program to Find the Transpose of a Square Matrix

Below is the C program to find the transpose of a square matrix:

        // C program to find the transpose of a square Matrix
#include <stdio.h>

// The order of the initial matrix is 3 x 3
#define size 3

// Function to transpose a Matrix
void transposeMatrix(int mat[][size], int transposeMatrix[][size])
{
 for (int i=0; i<size; i++)
 {
 for (int j=0; j<size; j++)
 {
 transposeMatrix[i][j] = mat[j][i];
 }
 }
}

// Driver Code
int main()
{
 int mat[size][size] = { {4, 2, 8},
 {9, 7, 1},
 {0, 2, 7} };

printf("Initial Matrix: \⁠n");

// Printing the initial Matrix
 for (int i = 0; i < size; i++)
 {
 for (int j = 0; j < size; j++)
 {
 printf("%d ", mat[i][j]);
 }
 printf("\⁠n");
 }

// Variable to store the transposed Matrix
 int transposedMatrix[size][size];

transposeMatrix(mat, transposedMatrix);

printf("Transposed Matrix: \⁠n");

// Printing the transposed Matrix
 for (int i = 0; i < size; i++)
 {
 for (int j = 0; j < size; j++)
 {
 printf("%d ", transposedMatrix[i][j]);
 }
 printf("\⁠n");
 }

return 0;
}

Output:

        Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7

Solve Basic Programming Problems Based on Matrices

A matrix is a grid used to store or display data in a structured format. Matrices are widely used in programming to perform various operations. If you're looking to cover all coding interview bases, you must know how to perform basic operations like addition, subtraction, multiplication, and more on matrices.