How to find the transpose of a matrix

Overview

A transpose of a matrix is an operator that flips a matrix diagonally. It does this by flipping the row and column indices of matrix A and producing a new matrix A'.

Transpose of matrix

Next, let’s see how we achieve the transpose of the matrix with the help of an example.

Example

#include <iostream>
using namespace std;
#define size 4
// This function stores transpose of MatixA[][] in TranposeOfA[][]
void transposeOfMatix(int MatixA[][size], int TranposeOfA[][size])
{
for (int row = 0; row < size; row++)
for (int column = 0; column < size; column++)
TranposeOfA[row][column] = MatixA[column][row];
}
int main()
{
int MatixA[size][size] = { {11, 12, 13, 13},
{21, 22, 23, 24},
{31, 32, 33, 34},
{41, 42, 43, 44}};
int TranposeOfA[size][size];
transposeOfMatix(MatixA, TranposeOfA);
cout <<" Matrix A is "<< endl;
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
cout <<" "<< MatixA[row][column];
}
cout <<endl;
}
cout<<"************************************************"<<endl;
cout <<" Tranpose of A matrix is "<< endl;
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
cout <<" "<< TranposeOfA[row][column];
}
cout <<endl;
}
return 0;
}

Explanation

  • Line 6: We implement the transposeOfMatix function.

  • Line 13–16: We declare and initialise MatixA.

  • Line 20: We call the transposeOfMatixfunction.

  • Line 21–29: We print MatixA.

  • Line 31–39: We print the TranposeOfA matrix.