📜  查找矩阵转置的程序

📅  最后修改于: 2022-05-13 01:57:22.326000             🧑  作者: Mango

查找矩阵转置的程序

矩阵的转置是通过将行更改为列和将列更改为行来获得的。换言之,A[][] 的转置是通过将 A[i][j] 更改为 A[j][i] 来获得的。

矩阵转置

对于方阵:

下面的程序找到 A[][] 的转置并将结果存储在 B[][] 中,我们可以将 N 更改为不同的维度。

C++
// C++ Program to find
// transpose of a matrix
#include 
using namespace std;
#define N 4
 
// This function stores transpose of A[][] in B[][]
void transpose(int A[][N], int B[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            B[i][j] = A[j][i];
}
 
// Driver code
int main()
{
    int A[N][N] = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};
 
    int B[N][N], i, j;
 
    transpose(A, B);
 
    cout <<"Result matrix is \n";
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
           cout <<" "<< B[i][j];
        cout <<"\n";
    }
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
// C Program to find
// transpose of a matrix
#include 
#define N 4
 
// This function stores transpose of A[][] in B[][]
void transpose(int A[][N], int B[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            B[i][j] = A[j][i];
}
 
int main()
{
    int A[N][N] = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};
 
    int B[N][N], i, j;
 
    transpose(A, B);
 
    printf("Result matrix is \n");
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
           printf("%d ", B[i][j]);
        printf("\n");
    }
 
    return 0;
}


Java
// Java Program to find
// transpose of a matrix
 
class GFG
{
    static final int N = 4;
     
    // This function stores transpose
    // of A[][] in B[][]
    static void transpose(int A[][], int B[][])
    {
        int i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < N; j++)
                B[i][j] = A[j][i];
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int A[][] = { {1, 1, 1, 1},
                      {2, 2, 2, 2},
                      {3, 3, 3, 3},
                      {4, 4, 4, 4}};
     
        int B[][] = new int[N][N], i, j;
     
        transpose(A, B);
     
        System.out.print("Result matrix is \n");
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < N; j++)
            System.out.print(B[i][j] + " ");
            System.out.print("\n");
        }
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 Program to find
# transpose of a matrix
 
N = 4
  
# This function stores
# transpose of A[][] in B[][]
 
def transpose(A,B):
 
    for i in range(N):
        for j in range(N):
            B[i][j] = A[j][i]
 
# driver code
A = [ [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4]]
  
  
B = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] # To store result
 
transpose(A, B)
  
print("Result matrix is")
for i in range(N):
    for j in range(N):
        print(B[i][j], " ", end='')
    print()
     
# This code is contributed
# by Anant Agarwal.


C#
// C# Program to find
// transpose of a matrix
using System;
 
class GFG
{
    static int N = 4;
     
    // This function stores transpose
    // of A[][] in B[][]
    static void transpose(int [,]A, int [,]B)
    {
        int i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < N; j++)
                B[i,j] = A[j,i];
    }
     
    // Driver code
    public static void Main ()
    {
        int [,]A = { {1, 1, 1, 1},
                     {2, 2, 2, 2},
                     {3, 3, 3, 3},
                     {4, 4, 4, 4}};
     
        int [,]B = new int[N,N];
         
        // Function calling
        transpose(A, B);
     
        Console.Write("Result matrix is \n");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            Console.Write(B[i,j] + " ");
            Console.Write("\n");
        }
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


C++
// C++ program to find
// transpose of a matrix
#include 
using namespace std;
#define M 3
#define N 4
 
// This function stores transpose
// of A[][] in B[][]
void transpose(int A[][N], int B[][M])
{
    int i, j;
    for(i = 0; i < N; i++)
        for(j = 0; j < M; j++)
            B[i][j] = A[j][i];
}
 
// Driver code
int main()
{
    int A[M][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 } };
 
    // Note dimensions of B[][]
    int B[N][M], i, j;
 
    transpose(A, B);
 
    cout << "Result matrix is \n";
    for(i = 0; i < N; i++)
    {
        for(j = 0; j < M; j++)
            cout << " " << B[i][j];
             
        cout << "\n";
    }
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
#include 
#define M 3
#define N 4
 
// This function stores transpose of A[][] in B[][]
void transpose(int A[][N], int B[][M])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < M; j++)
            B[i][j] = A[j][i];
}
 
int main()
{
    int A[M][N] = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3}};
 
    // Note dimensions of B[][]
    int B[N][M], i, j;
 
    transpose(A, B);
 
    printf("Result matrix is \n");
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < M; j++)
        printf("%d ", B[i][j]);
        printf("\n");
    }
 
    return 0;
}


Java
// Java Program to find
// transpose of a matrix
 
class GFG
{
    static final int M = 3;
    static final int N = 4;
     
    // This function stores transpose
    // of A[][] in B[][]
    static void transpose(int A[][], int B[][])
    {
        int i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < M; j++)
                B[i][j] = A[j][i];
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int A[][] = { {1, 1, 1, 1},
                      {2, 2, 2, 2},
                      {3, 3, 3, 3}};
     
        int B[][] = new int[N][M], i, j;
     
        transpose(A, B);
     
        System.out.print("Result matrix is \n");
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < M; j++)
            System.out.print(B[i][j] + " ");
            System.out.print("\n");
        }
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 Program to find
# transpose of a matrix
 
M = 3
N = 4
 
# This function stores
# transpose of A[][] in B[][]
 
def transpose(A, B):
 
    for i in range(N):
        for j in range(M):
            B[i][j] = A[j][i]
 
# driver code
A = [ [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3]]
 
 
# To store result
B = [[0 for x in range(M)] for y in range(N)]
 
transpose(A, B)
 
print("Result matrix is")
for i in range(N):
    for j in range(M):
        print(B[i][j], " ", end='')
    print()


C#
// C# Program to find
// transpose of a matrix
 using System;
class GFG {
 
    static  int M = 3;
    static  int N = 4;
      
    // This function stores transpose
    // of A[][] in B[][]
    static void transpose(int [,]A, int [,]B)
    {
        int i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < M; j++)
                B[i,j] = A[j,i];
    }
      
    // Driver code
    public static void Main ()
    {
        int [,]A = { {1, 1, 1, 1},
                      {2, 2, 2, 2},
                      {3, 3, 3, 3}};
      
        int [,]B= new int[N,M];
      
        transpose(A, B);
      
        Console.WriteLine("Result matrix is \n");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            Console.Write(B[i,j] + " ");
            Console.Write("\n");
        }
    }
}
  
// This code is contributed by nitin mittal


PHP


Javascript


C++
#include 
using namespace std;
 
#define N 4
 
// Converts A[][] to its transpose
void transpose(int A[][N])
{
    for (int i = 0; i < N; i++)
        for (int j = i+1; j < N; j++)
            swap(A[i][j], A[j][i]);
}
 
int main()
{
    int A[N][N] = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};
 
    transpose(A);
 
    printf("Modified matrix is \n");
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
           printf("%d ", A[i][j]);
        printf("\n");
    }
 
    return 0;
}


Java
// Java Program to find
// transpose of a matrix
 
class GFG
{
    static final int N = 4;
     
    // Finds transpose of A[][] in-place
    static void transpose(int A[][])
    {
        for (int i = 0; i < N; i++)
            for (int j = i+1; j < N; j++)
            {
                 int temp = A[i][j];
                 A[i][j] = A[j][i];
                 A[j][i] = temp;
            }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int A[][] = { {1, 1, 1, 1},
                      {2, 2, 2, 2},
                      {3, 3, 3, 3},
                      {4, 4, 4, 4}};
        
        transpose(A);
     
        System.out.print("Modified matrix is \n");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            System.out.print(A[i][j] + " ");
            System.out.print("\n");
        }
    }
}


Python3
# Python3 Program to find
# transpose of a matrix
 
N = 4
  
# Finds transpose of A[][] in-place
def transpose(A):
 
    for i in range(N):
        for j in range(i+1, N):
            A[i][j], A[j][i] = A[j][i], A[i][j]
 
# driver code
A = [ [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4]]
  
 transpose(A)
  
print("Modified matrix is")
for i in range(N):
    for j in range(N):
        print(A[i][j], " ", end='')
    print()
     
# This code is contributed
# by Anant Agarwal.


C#
// C# Program to find transpose of
// a matrix
using System;
 
class GFG {
     
    static int N = 4;
     
    // Finds transpose of A[][] in-place
    static void transpose(int [,]A)
    {
        for (int i = 0; i < N; i++)
            for (int j = i+1; j < N; j++)
            {
                int temp = A[i,j];
                A[i,j] = A[j,i];
                A[j,i] = temp;
            }
    }
     
    // Driver code
    public static void Main ()
    {
        int [,]A = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};
         
        transpose(A);
     
        Console.WriteLine("Modified matrix is ");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
                Console.Write(A[i,j] + " ");
                 
            Console.WriteLine();
        }
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出:

Result matrix is
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

对于矩形矩阵:

下面的程序找到 A[][] 的转置并将结果存储在 B[][] 中。

C++

// C++ program to find
// transpose of a matrix
#include 
using namespace std;
#define M 3
#define N 4
 
// This function stores transpose
// of A[][] in B[][]
void transpose(int A[][N], int B[][M])
{
    int i, j;
    for(i = 0; i < N; i++)
        for(j = 0; j < M; j++)
            B[i][j] = A[j][i];
}
 
// Driver code
int main()
{
    int A[M][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 } };
 
    // Note dimensions of B[][]
    int B[N][M], i, j;
 
    transpose(A, B);
 
    cout << "Result matrix is \n";
    for(i = 0; i < N; i++)
    {
        for(j = 0; j < M; j++)
            cout << " " << B[i][j];
             
        cout << "\n";
    }
    return 0;
}
 
// This code is contributed by shivanisinghss2110

C

#include 
#define M 3
#define N 4
 
// This function stores transpose of A[][] in B[][]
void transpose(int A[][N], int B[][M])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < M; j++)
            B[i][j] = A[j][i];
}
 
int main()
{
    int A[M][N] = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3}};
 
    // Note dimensions of B[][]
    int B[N][M], i, j;
 
    transpose(A, B);
 
    printf("Result matrix is \n");
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < M; j++)
        printf("%d ", B[i][j]);
        printf("\n");
    }
 
    return 0;
}

Java

// Java Program to find
// transpose of a matrix
 
class GFG
{
    static final int M = 3;
    static final int N = 4;
     
    // This function stores transpose
    // of A[][] in B[][]
    static void transpose(int A[][], int B[][])
    {
        int i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < M; j++)
                B[i][j] = A[j][i];
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int A[][] = { {1, 1, 1, 1},
                      {2, 2, 2, 2},
                      {3, 3, 3, 3}};
     
        int B[][] = new int[N][M], i, j;
     
        transpose(A, B);
     
        System.out.print("Result matrix is \n");
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < M; j++)
            System.out.print(B[i][j] + " ");
            System.out.print("\n");
        }
    }
}
 
// This code is contributed by Anant Agarwal.

Python3

# Python3 Program to find
# transpose of a matrix
 
M = 3
N = 4
 
# This function stores
# transpose of A[][] in B[][]
 
def transpose(A, B):
 
    for i in range(N):
        for j in range(M):
            B[i][j] = A[j][i]
 
# driver code
A = [ [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3]]
 
 
# To store result
B = [[0 for x in range(M)] for y in range(N)]
 
transpose(A, B)
 
print("Result matrix is")
for i in range(N):
    for j in range(M):
        print(B[i][j], " ", end='')
    print()   

C#

// C# Program to find
// transpose of a matrix
 using System;
class GFG {
 
    static  int M = 3;
    static  int N = 4;
      
    // This function stores transpose
    // of A[][] in B[][]
    static void transpose(int [,]A, int [,]B)
    {
        int i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < M; j++)
                B[i,j] = A[j,i];
    }
      
    // Driver code
    public static void Main ()
    {
        int [,]A = { {1, 1, 1, 1},
                      {2, 2, 2, 2},
                      {3, 3, 3, 3}};
      
        int [,]B= new int[N,M];
      
        transpose(A, B);
      
        Console.WriteLine("Result matrix is \n");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            Console.Write(B[i,j] + " ");
            Console.Write("\n");
        }
    }
}
  
// This code is contributed by nitin mittal

PHP


Javascript


输出:

Result matrix is
1 2 3 
1 2 3 
1 2 3 
1 2 3

就地矩阵:

C++

#include 
using namespace std;
 
#define N 4
 
// Converts A[][] to its transpose
void transpose(int A[][N])
{
    for (int i = 0; i < N; i++)
        for (int j = i+1; j < N; j++)
            swap(A[i][j], A[j][i]);
}
 
int main()
{
    int A[N][N] = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};
 
    transpose(A);
 
    printf("Modified matrix is \n");
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
           printf("%d ", A[i][j]);
        printf("\n");
    }
 
    return 0;
}

Java

// Java Program to find
// transpose of a matrix
 
class GFG
{
    static final int N = 4;
     
    // Finds transpose of A[][] in-place
    static void transpose(int A[][])
    {
        for (int i = 0; i < N; i++)
            for (int j = i+1; j < N; j++)
            {
                 int temp = A[i][j];
                 A[i][j] = A[j][i];
                 A[j][i] = temp;
            }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int A[][] = { {1, 1, 1, 1},
                      {2, 2, 2, 2},
                      {3, 3, 3, 3},
                      {4, 4, 4, 4}};
        
        transpose(A);
     
        System.out.print("Modified matrix is \n");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            System.out.print(A[i][j] + " ");
            System.out.print("\n");
        }
    }
}

Python3

# Python3 Program to find
# transpose of a matrix
 
N = 4
  
# Finds transpose of A[][] in-place
def transpose(A):
 
    for i in range(N):
        for j in range(i+1, N):
            A[i][j], A[j][i] = A[j][i], A[i][j]
 
# driver code
A = [ [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4]]
  
 transpose(A)
  
print("Modified matrix is")
for i in range(N):
    for j in range(N):
        print(A[i][j], " ", end='')
    print()
     
# This code is contributed
# by Anant Agarwal.

C#

// C# Program to find transpose of
// a matrix
using System;
 
class GFG {
     
    static int N = 4;
     
    // Finds transpose of A[][] in-place
    static void transpose(int [,]A)
    {
        for (int i = 0; i < N; i++)
            for (int j = i+1; j < N; j++)
            {
                int temp = A[i,j];
                A[i,j] = A[j,i];
                A[j,i] = temp;
            }
    }
     
    // Driver code
    public static void Main ()
    {
        int [,]A = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};
         
        transpose(A);
     
        Console.WriteLine("Modified matrix is ");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
                Console.Write(A[i,j] + " ");
                 
            Console.WriteLine();
        }
    }
}
 
// This code is contributed by anuj_67.

PHP


Javascript


输出:

Modified matrix is
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

注意 - Transpose的时间复杂度为 O(n + m),其中 n 是列数,m 是矩阵中非零元素的数量。使用单位矩阵作为参考矩阵的矩阵转置的计算时间是 O(m*n)。假设,如果给定矩阵是方阵,则运行时间将为 O(n2)。