Java教程

Java转置矩阵的程序

将矩阵的行转换为列并将矩阵的列转换为行称为矩阵的转置。
让我们看看一个简单的示例,将3行3列的矩阵转置。
public class MatrixTransposeExample{
    public static void main(String args[]){
        //creating a matrixint original[][]={
            {1,3,4},
            {2,4,3},
            {3,4,5}
        };
        //creating another matrix to store transpose of a matrixint transpose[][]=new int[3][3];
        //3 rows and 3 columns
        //Code to transpose a matrixfor(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                transpose[i][j]=original[j][i];
            }
        }
        System.out.println("Printing Matrix without transpose:");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.print(original[i][j]+" ");
            }
            System.out.println();
        //new line }
        System.out.println("Printing Matrix After Transpose:");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.print(transpose[i][j]+" ");
            }
            System.out.println();
        //new line }
    }
}
输出:
Printing Matrix without transpose:
1 3 4
2 4 3
3 4 5
Printing Matrix After Transpose:
1 2 3
3 4 4
4 3 5

用于显示转置矩阵的Java程序

让我们再来看一个显示矩阵转置的示例。在这里,我们不会创建另一个矩阵。
public class MatrixTransposeExample2{
    public static void main(String args[]){
        //creating a matrixint original[][]={
            {1,3,4},
            {2,4,3},
            {3,4,5}
        };
        System.out.println("Printing Matrix without transpose:");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.print(original[i][j]+" ");
            }
            System.out.println();
        //new line }
        System.out.println("Printing Matrix After Transpose:");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.print(original[j][i]+" ");
            }
            System.out.println();
        //new line }
    }
}
输出:
Printing Matrix without transpose:
1 3 4
2 4 3
3 4 5
Printing Matrix After Transpose:
1 2 3
3 4 4
4 3 5

用于转置矩阵的Java程序(另一种方式)

您还可以使用未预定义矩阵值的方法。在这里,用户必须将值作为输入。
import java.util.Scanner;
public class MatrixTransposeExample2 {
    public static void main(String args[]) {
        int i, j;
        System.out.println("Enter total rows and columns: ");
        Scanner s = new Scanner(System.in);
        int row = s.nextInt();
        int column = s.nextInt();
        int array[][] = new int[row][column];
        System.out.println("Enter matrix:");
        for(i = 0;i <row;i++) {
            for(j = 0;j <column;j++) {
                array[i][j] = s.nextInt();
                System.out.print(" ");
            }
        }
        System.out.println("The above matrix before Transpose is ");
        for(i = 0;i <row;i++) {
            for(j = 0;j <column;j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println("");
        }
        System.out.println("The above matrix after Transpose is ");
        for(i = 0;i <column;i++) {
            for(j = 0;j <row;j++) {
                System.out.print(array[j][i]+" ");
            }
            System.out.println("");
        }
    }
}
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4