zoukankan      html  css  js  c++  java
  • java 矩阵转置算法

    工作中用到了行列转置,把这两种情况的算法记下来,以便后用

    1.行列数相等的转置

     1 /**
     2  * @description 矩阵转置
     3  * @author oldmonk
     4  * @time   2017年8月18日
     5  */
     6 public class test {
     7     
     8     public static void main(String [] args) {
     9         int data [][] = new int [] [] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } ;
    10         System.out.println("----------------转置前------------------------") ;
    11         print1(data) ;
    12         reverse(data) ;
    13         System.out.println("----------------转置后------------------------") ;
    14         print1(data) ;
    15     }
    16     
    17     // 将矩阵转置
    18     public static void reverse(int temp [][]) {
    19         for (int i = 0; i < temp.length; i++) {
    20             for (int j = i; j < temp[i].length; j++) {
    21                 int k = temp[i][j] ;
    22                 temp[i][j] = temp[j][i] ;
    23                 temp[j][i] = k ;
    24             }
    25         }
    26     }
    27     
    28     // 将矩阵输出
    29     public static void print1(int temp [][]) {
    30         for (int i = 0; i < temp.length; i++) {
    31             for (int j = 0; j < temp[i].length; j++) {
    32                 System.out.print(temp[i][j] + "	") ;
    33             }
    34             System.out.println() ;
    35         }
    36     }
    37 }

       测试结果:

       

    2.任意数组转置

     1 /**
     2  * @description 任意数组转置
     3  * @author oldmonk
     4  * @time   2017年8月18日
     5  */
     6 public class test2 {
     7     
     8     public static void main(String [] args)// 测试
     9     {
    10         double [][] TestMatrix = { { 1, 22, 34, 22 }, { 1, 11, 5, 21 }, { 7, 2, 13, 19 } } ;
    11         double [][] MatrixC = Transpose(TestMatrix, 3, 4) ;
    12         
    13         System.out.println("-------转置前---------") ;
    14         myPrint(TestMatrix);
    15         System.out.println("-------转置后---------") ;
    16         myPrint(MatrixC);
    17     }
    18     
    19     /**
    20      * @descript  任意二维数组转置
    21      * @author    xujingyang
    22      * @time      2017年8月17日
    23      */
    24     public static double [][] Transpose(double [][] Matrix, int Line, int List) {
    25         double [][] MatrixC = new double [List] [Line] ;
    26         for (int i = 0; i < Line; i++) {
    27             for (int j = 0; j < List; j++) {
    28                 MatrixC[j][i] = Matrix[i][j] ;
    29             }
    30         }
    31         return MatrixC ;
    32     }
    33     
    34     // 将矩阵输出
    35     public static void myPrint(double temp [][]) {
    36         for (int i = 0; i < temp.length; i++) {
    37             for (int j = 0; j < temp[i].length; j++) {
    38                 System.out.print(temp[i][j] + "	") ;
    39             }
    40             System.out.println() ;
    41         }
    42     }
    43 }

     测试结果:

       

     

     

     

  • 相关阅读:
    Shell基础
    个人对JavaScript预编译的理解
    文件系统管理
    文件特殊权限
    权限管理ACL权限
    用户和用户组管理
    RPM包管理-yum管理
    oracle11g完全卸载方法
    JVM概述
    复杂查询优质习题
  • 原文地址:https://www.cnblogs.com/xujingyang/p/7387843.html
Copyright © 2011-2022 走看看