Java查找给定矩阵的转置
 
 
 在此程序中,我们需要找到给定矩阵的转置并打印结果矩阵。
 
矩阵的转置
 
 可以通过将行与列互换来找到矩阵的转置,也就是说,原始矩阵的行将成为新矩阵的列。同样,原始矩阵中的列将成为新矩阵中的行。该操作可以表示为: 
 
 
 
 如果原始矩阵的尺寸为2×3,则新的转置矩阵的尺寸将为3×2。
 
 
  
  [ AT ]<sub>ij</sub>=[ A ]<sub>ji</sub>
  
 
 
  
算法
 
 步骤1:  START  
 步骤2: 定义rows,cols 
 步骤3: 初始化矩阵a[][] = {{1,2,3},{4,5,6},{7,8,9}}  
 步骤4: rows= a.length  
 步骤5:  cols = a[0].length  
 步骤6:  t[][] = [cols][rows]  
 步骤7: 直到将i < cols 
 ////for(i = 0; i < cols; i++) 重复步骤8至步骤9 
 步骤8: 重复步骤9,直到j < rows 
 ////for(j = 0; j < rows; j++) 
 步骤9:  t[i][j] = a[j][i]  
 步骤10: 打印"给定矩阵的转置"  
 步骤11: 直到将i < cols 
 ////for(i = 0; i < cols; i++) 重复步骤12至步骤14 
 步骤12: 重复步骤13,直到j < rows 
 ////for(j = 0; j < rows; j++) 
 步骤13: 打印t[i][j]  
 步骤14: 打印新行 
  STEP 15:  END  
程序
 
 
  
   public class Transpose 
 {
     public static void main(String[] args) {
         int rows, cols;
         //Initialize matrix a 
           int a[][] = {
             {1, 2, 3}, 
             {4, 5, 6}, 
             {7, 8, 9}
         };
         //Calculates number of rows and columns present in given matrix 
           rows = a.length;
         cols = a[0].length;
         //Declare array t with reverse dimensions 
         int t[][] = new int[cols][rows];
         //Calculates transpose of given matrix 
         for(int i = 0;i <cols;i++){
             for(int j = 0;j <rows;j++){
                 //Converts the row of original matrix into column of transposed matrix 
                 t[i][j] = a[j][i];
             }
         }
         System.out.println("Transpose of given matrix: ");
         for(int i = 0;i <cols;i++){
             for(int j = 0;j <rows;j++){
                 System.out.print(t[i][j] + " ");
             }
             System.out.println();
         }
     }
 }
  
 
 
  
 
 输出:  
 
 
  
   Transpose of given matrix
 1 4 7
 2 5 8
 3 6 9