Transposition d'une matrice en Java
Transposition d'une matrice en Java | Programme Java pour transposer un tableau 2D | Dans cet article, nous discuterons de ce qu'est la transposée d'une matrice et comment écrire un programme Java pour trouver la transposée d'une matrice ?
Qu'est-ce que la transposée d'une matrice ?
Soit A =[aij ] une matrice m × n. La transposée de A, notée A t , est la matrice n × m obtenue en intervertissant les lignes et les colonnes de A. Autrement dit, si A t =[bij ], alors bij =aji pour i =1,2,…,n et j =1,2,…,m.
Pour la matrice 3×2,
Matrice d'originea11 a12
a21 a22
a31 a32
Matrice de transpositiona11 a21 a31
a12 a22 a32
Exemple utilisant une matrice 2×2 :-
1 2
A =
3 4
Puis la transposée d'une matrice,
1 3
At =
2 4
Méthode Java pour trouver la transposition d'une Matrice
// method to calculate the transpose of a matrix
public static int[][] transposeMatrix(int[][] a) {
// calculate row and column size
int row = a.length;
int column = a[0].length;
// declare a matrix to store resultant
int temp[][] = new int[row][column];
// calculate transpose of matrix
// outer loop for row
for (int i = 0; i < row; i++) {
// inner loop for column
for (int j = 0; j < column; j++) {
// formula
temp[i][j] = a[j][i];
}
}
// return resultant matrix
return temp;
}
Complexité temporelle :- O(N 2 )
En supposant que la matrice est une matrice carrée, la taille de la ligne et de la colonne sera similaire. Ensuite, la méthode Java ci-dessus utilise deux boucles (de 1 à n) pour trouver la transposition de la matrice, donc la complexité temporelle de la méthode est O(N 2 ).
Programme Java pour trouver la transposée d'une matrice
import java.util.Arrays;
public class Matrix {
// main method
public static void main(String[] args) {
// declare and initialize a matrix
int a[][] = { { 1, 2 }, { 8, 9 } };
// find row and column size
int row = a.length;
int column = a[0].length;
// declare new matrix to store result
int transpose[][] = new int[row][column];
// Transpose of matrix
transpose = transposeMatrix(a);
// display all matrices
System.out.println("A = " + Arrays.deepToString(a));
System.out.println("Transpose = " +
Arrays.deepToString(transpose));
}
// method to calculate the transpose of a matrix
public static int[][] transposeMatrix(int[][] a) {
// calculate row and column size
int row = a.length;
int column = a[0].length;
// declare a matrix to store resultant
int temp[][] = new int[row][column];
// calculate transpose of matrix
// outer loop for row
for (int i = 0; i < row; i++) {
// inner loop for column
for (int j = 0; j < column; j++) {
// formula
temp[i][j] = a[j][i];
}
}
// return resultant matrix
return temp;
}
}
Sortie :-
A =[[
1, 2]
, [
8, 9]]
Transposer =[[
1, 8]
, [
2, 9]]
Dans ce programme, pour afficher la matrice, nous avions utilisé la méthode deepToString() de la classe Arrays, mais vous pouvez également utiliser les boucles imbriquées. Voir :- Différentes façons d'imprimer un tableau en Java
Programmer en prenant l'entrée de l'utilisateur
Dans le programme ci-dessus, les deux matrices A et B ont été initialisées dans le programme, voyons maintenant un autre programme pour transposer une matrice en prenant la valeur d'entrée de l'utilisateur final à l'aide de la classe Scanner. Si vous le souhaitez, vous pouvez également utiliser la classe BufferedReader.
import java.util.Scanner;
public class Matrix {
// main method
public static void main(String[] args) {
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// declare variables
int row = 0;
int column = 0;
int a[][] = null; // first matrix
int transpose[][] = null; // resultant matrix
// ask row and column size
System.out.println("Enter row and column size: ");
row = scan.nextInt();
column = scan.nextInt();
// initialize matrices
a = new int[row][column];
transpose = new int[row][column];
// read matrix A
System.out.println("Enter Matrix A: ");
for(int i=0; i<row; i++) {
for(int j=0; j<column; j++) {
// read matrix elements
a[i][j] = scan.nextInt();
}
}
// transpose of matrix
transpose = transposeMatrix(a);
// display resultant matrix
System.out.println("Transpose =");
for(int i=0; i<transpose.length; i++) {
for(int j=0; j<transpose[0].length; j++) {
System.out.print(transpose[i][j]+" ");
}
System.out.println(); // new line
}
// close Scanner
scan.close();
}
// method to calculate the transpose of a matrix
public static int[][] transposeMatrix(int[][] a) {
// calculate row and column size
int row = a.length;
int column = a[0].length;
// declare a matrix to store resultant
int temp[][] = new int[row][column];
// calculate transpose of matrix
// outer loop for row
for (int i = 0; i < row; i++) {
// inner loop for column
for (int j = 0; j < column; j++) {
// formula
temp[i][j] = a[j][i];
}
}
// return resultant matrix
return temp;
}
}
Sortie :-
Entrez la taille des lignes et des colonnes :
3 3
Entrez la matrice A :
1 2 3
4 5 6
7 8 9
Transposer =
1 4 7
2 5 8
3 6 9
Dans la méthode principale, nous avions d'abord créé l'objet de classe Scanner pour lire la valeur d'entrée. Ensuite, nous avions initialisé les variables requises. Ensuite, les valeurs de ligne et de colonne ont été lues par l'utilisateur final. La matrice ultérieure est initialisée avec la valeur par défaut et à côté de cela, les éléments de la matrice sont remplis dans la matrice en prenant les valeurs d'entrée de l'utilisateur final. Après cela, la méthode transposeMatrix() est appelée, ce qui renvoie la transposition de la matrice ou du tableau 2D passé. Enfin, les matrices sont affichées à l'écran.
Voir plus de programmes matriciels en Java :-
- Programme d'impression de matrice 3 x 3
- Somme des éléments de matrice en Java
- Somme des éléments diagonaux de la matrice en Java
- Somme des lignes et somme des colonnes de la matrice en Java
- Ajout de matrice en Java
- Soustraction de deux matrices en Java
- Multiplication matricielle en Java
- Programme piloté par menus pour les opérations Matrix