21xrx.com
2024-04-26 07:51:48 Friday
登录
文章检索 我的文章 写文章
Java中矩阵的转置
2021-07-08 17:54:51 深夜i     --     --
J a v a

Java 程序找到矩阵(任意顺序)的转置,我们交换它的行和列以获得转置。

Java中的矩阵转置

import java.util.Scanner;


class TransposeAMatrix
{
  public static void main(String args[])
  {
    int m, n, c, d;

    Scanner in = new Scanner(System.in);
    System.out.println("Enter the number of rows and columns of matrix");
    m = in.nextInt();
    n = in.nextInt();

    int matrix[][] = new int[m][n];

    System.out.println("Enter elements of the matrix");

    for (c = 0; c < m; c++)
      for (d = 0; d < n; d++)
        matrix[c][d] = in.nextInt();

    int transpose[][] = new int[n][m];

    for (c = 0; c < m; c++)
      for (d = 0; d < n; d++)
        transpose[d][c] = matrix[c][d];

    System.out.println("Transpose of the matrix:");

    for (c = 0; c < n; c++)
    {
      for (d = 0; d < m; d++)
        System.out.print(transpose[c][d]+"\t");

      System.out.print("\n");
    }
  }
}

下载转置矩阵程序类文件。

程序输出:

该程序可用于检查矩阵是否对称。 我们将矩阵与其转置进行比较,如果两者相同,则它是对称的,否则是非对称的。 它也可用于计算矩阵的正交性。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复