Java Matrix Transpose transpose(int[][] M)

Here you can find the source of transpose(int[][] M)

Description

Takes the transpose of an array (like the matrix operation).

License

Open Source License

Exception

Parameter Description
IllegalArgumentException if the arrayis not a matrix

Declaration

public static int[][] transpose(int[][] M) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// w  w  w  .  ja v  a 2s  . c  o  m
     * Takes the transpose of an array (like the matrix operation).
     *
     * @throws IllegalArgumentException if the array is not a matrix
     */
    public static double[][] transpose(double[][] M) {
        double[][] Mt = new double[M[0].length][M.length];
        for (int i = 0; i < M.length; i++) {
            if (M[i].length != M[0].length) {
                throw new IllegalArgumentException("The array is not a matrix.");
            }
            for (int j = 0; j < M[0].length; j++) {
                Mt[j][i] = M[i][j];
            }
        }
        return (Mt);
    }

    /**
     * Takes the transpose of an array (like the matrix
     * operation).
     *
     * @throws IllegalArgumentException if the array
     *                                  is not a matrix
     */
    public static int[][] transpose(int[][] M) {
        int[][] Mt = new int[M[0].length][M.length];
        for (int i = 0; i < M.length; i++) {
            if (M[i].length != M[0].length) {
                throw new IllegalArgumentException("The array is not a matrix.");
            }
            for (int j = 0; j < M[0].length; j++) {
                Mt[j][i] = M[i][j];
            }
        }
        return (Mt);
    }
}

Related

  1. transpose(final double[][] src, final double[][] dest, int l1, int l2)
  2. transpose(float[] mat, float[] dest)
  3. transpose(float[][] data)
  4. transpose(int N, double src[][])
  5. transpose(int[][] input)
  6. transpose(int[][] matrix)
  7. transpose(int[][][] as, int A, int[][][] ast)
  8. transpose(long[] a, double offset)
  9. transpose(Object[][] matrix)