Here you can find the source of transpose(int[][] M)
Parameter | Description |
---|---|
IllegalArgumentException | if the arrayis not a matrix |
public static int[][] transpose(int[][] M)
//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); } }