transpose double[][] matrix with a java function?

后端 未结 6 1541
夕颜
夕颜 2020-12-03 05:55

Do anybody have a function with which I can transpose a Matrix in Java which has the following form:

double[][]

I have function like this:<

相关标签:
6条回答
  • 2020-12-03 06:30

    Java Class to Transpose a matrix :-

    import java.util.Scanner;
    
    public class Transpose {
    
        /**
         * @param args
         */
    
        static int col;
        static int row;
        static int[][] trans_arr = new int[col][row];
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Scanner sc = new Scanner(System.in);
            int m = sc.nextInt();
            col = m;
            int n = sc.nextInt();
            row = n;
    
            int[][] arr = new int[row][col];
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    arr[i][j] = sc.nextInt();
                    System.out.print(arr[i][j] + " ");
                }
                System.out.println();
            }
            int[][] trans_arr = new int[col][row];
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    trans_arr[j][i] = arr[i][j];
                }
            }
            for (int i = 0; i < col; i++) {
                for (int j = 0; j < row; j++) {
                    System.out.print(trans_arr[i][j] + " ");
    
                }
                System.out.println();
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 06:34

    Since Java 8, you can do this:

    public static double[][] transposeMatrix(final double[][] matrix) {
        return IntStream.range(0, matrix[0].length)
            .mapToObj(i -> Stream.of(matrix).mapToDouble(row -> row[i]).toArray())
            .toArray(double[][]::new);
    }
    
    0 讨论(0)
  • 2020-12-03 06:36

    Here is the method

    public static double[][] transpose(double arr[][]){
        int m = arr.length;
        int n = arr[0].length;
        double ret[][] = new double[n][m];
    
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                ret[j][i] = arr[i][j];
            }
        }
    
        return ret;
    }
    
    0 讨论(0)
  • 2020-12-03 06:39

    Here's a small change!

    for (int j = i; j < m[0].length; j++)
    
    0 讨论(0)
  • 2020-12-03 06:42

    If you would like to use an external library, Apache Commons Math provides the utility to transpose a matrix. Please refer to it official site.

    First, you have to create a double array double[][] arr, as you have already done. Then, the transposed 2d matrix can be achieved like this

    MatrixUtils.createRealMatrix(arr).transpose().getData()
    
    0 讨论(0)
  • 2020-12-03 06:46
        public static double[][] transposeMatrix(double [][] m){
            double[][] temp = new double[m[0].length][m.length];
            for (int i = 0; i < m.length; i++)
                for (int j = 0; j < m[0].length; j++)
                    temp[j][i] = m[i][j];
            return temp;
        }
    
    0 讨论(0)
提交回复
热议问题