English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce programme, vous apprendrez à rechercher et à imprimer la transposition d'une matrice donnée en Java.
La transposition d'une matrice consiste à échanger les lignes contre les colonnes. Pour2x3matrice ,
matrice a11 a12 a13 a21 a22 a23 Matrice transposée a11 a21 a12 a22 a13 a23
public class Transpose { public static void main(String[] args) { int row = 2, column = 3; int[][] matrix = { {2, 3, 4}, {5, 6, 4}; //Affichage de la matrice actuelle display(matrix); //Matrice transposée int[][] transpose = new int[column][row]; for (int i = 0; i < row;++) { for (int j = 0; j < column;++) { transpose[j][i] = matrix[i][j]; } } //Affichage de la matrice transposée display(transpose);}} } public static void display(int[][] matrix) { System.out.println("The matrix is: "); for (int[] row : matrix) { for (int column : row) { System.out.print(column + " "); } System.out.println(); } } }
When running the program, the output is:
The matrix is: 2 3 4 5 6 4 The matrix is: 2 5 3 6 4 4
In the above program, the display() function is only used to print the content of the matrix to the screen.
Here, the given matrix is in the form of2x3That is, row = 2 and column = 3.
For the transpose matrix, we change the transpose order to3x2, that is, row = 3 and column = 2. Therefore, we have transpose = int[column][row]
The transpose of a matrix is calculated by simply exchanging columns for rows:
transpose[j][i] = matrix[i][j];