The transpose of a matrix is just a flipped version of
the original matrix. We can transpose a matrix by switching
its rows with its columns. The original rows become the new columns
and the original columns become the new rows.
We denote the transpose of matrix A by AT.
For example:
1 2 3 1 4 7
A = 4 5 6 then AT = 2 5 8
7 8 9 3 6 9
Similarly if
B = 1 2 3 then BT = 1 4
4 5 6 2 5
3 6
// Transpose a matrix
fn matrix_transpose(m: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut t = vec![Vec::with_capacity(m.len()); m[0].len()];
for r in m {
for i in 0..r.len() {
t[i].push(r[i]);
}
}
t
}
fn main() {
let m = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
println!("Matrix:
{:?}", m);
let t = matrix_transpose(m);
println!("Transpose:
{:?}", t);
}
public class Solution
{
public int[][] Transpose(int[][] matrix)
{
var trans = GenerateArray(matrix[0].Length,matrix.Length,0);
for(var i=0; i<matrix.Length;i++)
{
for(var j=0; j<matrix[0].Length;j++)
{
trans[j][i] = matrix[i][j];
}
}
return trans;
}
public static T[][] GenerateArray<T>(int row, int Col,T value)
{
var arr = new T[row][];
for (int i = 0; i < row; i++)
{
arr[i] = new T[Col];
for (int j = 0; j < Col; j++)
{
arr[i][j] = value;
}
}
return arr;
}
}
#include<stdio.h>
void main(){
int row,column,i,j,mat[100][100],tra[100][100];
printf("Enter number of rows of matrix : ");
scanf("%d",&row);
printf("Enter number of columns of matrix : ");
scanf("%d",&column);
printf("#### Matrix ####
");
for(i=0;i<row;i++){
for(j=0;j<column;j++){
printf("Enter element a %d%d : ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
}
for(i=0;i<row;i++){
for(j=0;j<column;j++){
tra[i][j] = mat[j][i];
}
}
for(i=0;i<row;i++){
for(j=0;j<column;j++){
printf("%d ",tra[i][j]);
}
printf("
");
}
}