xxxxxxxxxx
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
xxxxxxxxxx
// 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:\n{:?}", m);
let t = matrix_transpose(m);
println!("Transpose:\n{:?}", t);
}
xxxxxxxxxx
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;
}
}
xxxxxxxxxx
function transposeMatrix(matrix) {
const transposed = matrix.reduce((result, row) => {
row.forEach((value, column) => {
if (!result[column]) {
result[column] = [];
}
result[column].push(value);
});
return result;
}, []);
return transposed;
}
// Example usage
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const transposedMatrix = transposeMatrix(matrix);
console.log(transposedMatrix);
xxxxxxxxxx
function transposeMatrix(matrix) {
if (matrix.length === 0 || matrix[0].length === 0) {
return [];
}
const transposed = [];
for (let column = 0; column < matrix[0].length; column++) {
const newRow = [];
for (let row = 0; row < matrix.length; row++) {
newRow.push(matrix[row][column]);
}
transposed.push(newRow);
}
return [transposed[0]].concat(transposeMatrix(transposed.slice(1)));
}
// Example usage
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const transposedMatrix = transposeMatrix(matrix);
console.log(transposedMatrix);
xxxxxxxxxx
package com.company.Array;
public class MatrixTransponse {
public static void main (String[] args) {
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
print(matrix);
int row = matrix.length;
int columns = matrix[0].length;
int [][] mat = new int[columns][row];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
mat[j][i] = matrix[i][j];
}
}
print(mat);
}
public static void print(int[][] matrix){
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
System.out.println("\n");
}
}
xxxxxxxxxx
import java.util.*;
public class MatrixTranspose {
public static void main(String[] args) {
Scanner Input = new Scanner (System.in);
System.out.println("Enter number of rows");
int row = Input.nextInt();
System.out.println("Enter number of columns");
int column = Input.nextInt();
int [][] Matrix = new int [row][column];
System.out.println("Enter "+(row*column)+" values:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
Matrix[i][j]= Input.nextInt();
}
}
System.out.println("Before");
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
System.out.print(Matrix[i][j]+ "\t");
}
System.out.println("");
}
System.out.println("After");
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
System.out.print(Matrix[j][i]+ "\t");
}
System.out.println("");
}
}
}
xxxxxxxxxx
#include <stdio.h>
#include <stdlib.h>
void main()
{
int row, col;
scanf("%d %d", &row, &col);
int *matrix = malloc(col * row * sizeof(int));
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
scanf("%d", (matrix + i * col + j));
}
}
for (int i = 0; i < col; i++)
{
for (int j = 0; j < row; j++)
{
printf("%d ", *(matrix + j * col + i));
}
printf("\n");
}
}