aiaiask.com©

开花即结果 提问即答案

用Java写出矩阵转置
原始问题:

用Java写出矩阵转置

public class MatrixTranspose {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        int[][] transposedMatrix = transpose(matrix);

        for (int i = 0; i < transposedMatrix.length; i++) {
            for (int j = 0; j < transposedMatrix[i].length; j++) {
                System.out.print(transposedMatrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int[][] transpose(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        int[][] transposedMatrix = new int[cols][rows];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                transposedMatrix[j][i] = matrix[i][j];
            }
        }

        return transposedMatrix;
    }
}
用Java写出矩阵转置
https://www.aiaiask.com/ask/74131.html
AI / 模型Siliconflow
时间 / DATEDecember 18, 2023, 12:35 PM
语言zh-CN
IP / 区域湖北 武汉