Program to Compute Transpose of Matrix

Program to Compute Transpose of Matrix

  • Write a program to Compute Transpose of Matrix in C
  • Write a program to Compute Transpose of Matrix in C++
  • Write a program to Compute Transpose of Matrix in Python
  • Write a program to Compute Transpose of Matrix in PHP
  • Write a program to Compute Transpose of Matrix in Java
  • Write a program to Compute Transpose of Matrix in Java Script
  • Write a program to Compute Transpose of Matrix in C#

Explanation:

The rows of the old matrix are converted to the new matrix’s columns in order to calculate a matrix’s transpose. In particular, the element in the original matrix at position A[i][j] becomes A[j][i] in the transposed matrix.

Steps:

  1. Input:
    • Read an m×nm \times nm×n matrix.
  2. Initialize:
    • Create an empty matrix for the transpose, which will be of size n×mn \times mn×m (the number of rows becomes the number of columns and vice versa).
  3. Transpose the Matrix:
    • For each element A[i][j] in the original matrix:
      • Set A′[j][i] = A[i][j] in the transposed matrix.
  4. Output:
    • Print the transposed matrix.

Program to Compute Transpose of Matrix

#include <stdio.h>

void computeTranspose(int matrix[3][3], int transpose[3][3], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }
}

void printMatrix(int matrix[3][3], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    int transpose[3][3];
    int n = 3;

    computeTranspose(matrix, transpose, n);

    printf("Original Matrix:\n");
    printMatrix(matrix, n);

    printf("\nTranspose Matrix:\n");
    printMatrix(transpose, n);

    return 0;
}

#include <iostream>
using namespace std;

void computeTranspose(int matrix[3][3], int transpose[3][3], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }
}

void printMatrix(int matrix[3][3], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    int transpose[3][3];
    int n = 3;

    computeTranspose(matrix, transpose, n);

    cout << "Original Matrix:" << endl;
    printMatrix(matrix, n);

    cout << "\nTranspose Matrix:" << endl;
    printMatrix(transpose, n);

    return 0;
}

def compute_transpose(matrix):
    n = len(matrix)
    transpose = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            transpose[j][i] = matrix[i][j]
    return transpose

def print_matrix(matrix):
    for row in matrix:
        print(" ".join(map(str, row)))

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

transpose = compute_transpose(matrix)

print("Original Matrix:")
print_matrix(matrix)

print("\nTranspose Matrix:")
print_matrix(transpose)

<?php
function computeTranspose($matrix, $n) {
    $transpose = array();
    for ($i = 0; $i < $n; $i++) {
        for ($j = 0; $j < $n; $j++) {
            $transpose[$j][$i] = $matrix[$i][$j];
        }
    }
    return $transpose;
}

function printMatrix($matrix, $n) {
    for ($i = 0; $i < $n; $i++) {
        for ($j = 0; $j < $n; $j++) {
            echo $matrix[$i][$j] . " ";
        }
        echo "\n";
    }
}

$matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
$n = 3;

$transpose = computeTranspose($matrix, $n);

echo "Original Matrix:\n";
printMatrix($matrix, $n);

echo "\nTranspose Matrix:\n";
printMatrix($transpose, $n);
?>

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-java">public class TransposeMatrix {
    public static int[][] computeTranspose(int[][] matrix, int n) {
        int[][] transpose = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                transpose[j][i] = matrix[i][j];
            }
        }
        return transpose;
    }

    public static void printMatrix(int[][] matrix, int n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        int n = 3;

        int[][] transpose = computeTranspose(matrix, n);

        System.out.println("Original Matrix:");
        printMatrix(matrix, n);

        System.out.println("\nTranspose Matrix:");
        printMatrix(transpose, n);
    }
}

function computeTranspose(matrix) {
    const n = matrix.length;
    const transpose = Array.from({ length: n }, () => Array(n).fill(0));
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }
    return transpose;
}

function printMatrix(matrix) {
    matrix.forEach(row => console.log(row.join(" ")));
}

const matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

const transpose = computeTranspose(matrix);

console.log("Original Matrix:");
printMatrix(matrix);

console.log("\nTranspose Matrix:");
printMatrix(transpose);

using System;

class TransposeMatrix {
    static int[,] ComputeTranspose(int[,] matrix, int n) {
        int[,] transpose = new int[n, n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                transpose[j, i] = matrix[i, j];
            }
        }
        return transpose;
    }

    static void PrintMatrix(int[,] matrix, int n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }
    }

    static void Main() {
        int[,] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        int n = 3;

        int[,] transpose = ComputeTranspose(matrix, n);

        Console.WriteLine("Original Matrix:");
        PrintMatrix(matrix, n);

        Console.WriteLine("\nTranspose Matrix:");
        PrintMatrix(transpose, n);
    }
}

List of All Programs