I have a row-based multidimensional array:
/** [row][column]. */
public int[][] tiles;
I would like to transform this array to column-based
Here is my 50 cents: a utility method and test to transponse multidimensional array (for doubles in my case):
/**
* Transponse bidimensional array.
*
* @param original Original table.
* @return Transponsed.
*/
public static double[][] transponse(double[][] original) {
double[][] transponsed = new double
[original[0].length]
[original.length];
for (int i = 0; i < original[0].length; i++) {
for (int j = 0; j < original.length; j++) {
transponsed[i][j] = original[j][i];
}
}
return transponsed;
}
@Test
void aMatrix_OfTwoDimensions_ToBeTransponsed() {
final double[][] original =
new double[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
double[][] transponsed = Analysis.transponse(original);
assertThat(transponsed[1][2], is(equalTo(10)));
}
I saw that all of the answers create a new resultant matrix. This is simple: matrix[i][j] = matrix[j][i];
However, you can also do this in-place, in case of square matrix.
// Transpose, where m == n
for(int i = 0; i < m; i++) {
for(int j = i+1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
This is better for larger matrices, where creating a new resultant matrix is wasteful in terms of memory. If its not square, you can create a new one with NxM dimensions and do the out of place method. Note: for in-place, take care of j = i+1; Its not 0.
import java.util.Arrays;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int rows = askArray("Enter number of rows :", input);//asking number of rows from user
int columns = askArray("Enter number of columns :", input);//asking number of columns from user
int[][] array = Array(rows, columns, input);
DisplayArray(array, rows, columns);//displaying initial matrix
System.out.println("Transpose array ");
int[][] array2=TransposeArray(array, rows,columns );//calling Transpose array method
for (int i = 0; i < array[0].length; i++) {
System.out.println(Arrays.toString(array2[i]));
}
}
//method to take number of rows and number of columns from the user
public static int askArray(String s, Scanner in) {
System.out.print(s);
int value = in.nextInt();
return value;
}
//feeding elements to the matrix
public static int[][] Array(int x, int y, Scanner input) {
int[][] array = new int[x][y];
for (int j = 0; j < x; j++) {
System.out.print("Enter row number " + (j + 1) + ":");
for (int i = 0; i < y; i++) {
array[j][i] = input.nextInt();
}
}
return array;
}
//Method to display initial matrix
public static void DisplayArray(int[][] arra, int x, int y) {
for (int i = 0; i < x; i++) {
System.out.println(Arrays.toString(arra[i]));
}
}
//Method to transpose matrix
public static int[][] TransposeArray(int[][] arr,int x,int y){
int[][] Transpose_Array= new int [y][x];
for (int i = 0; i < x; i++) {
for (int j = 0; j <y ; j++) {
Transpose_Array[j][i]=arr[i][j];
}
}
return Transpose_Array;
}
}
Use this function (replace String by int if necessary). It takes the matrix as string array and returns a new matrix which is the transpose. It checks for the edge case of an empty array, too. No prints.
private String[][] transposeTable(String[][] table) {
// Transpose of empty table is empty table
if (table.length < 1) {
return table;
}
// Table isn't empty
int nRows = table.length;
int nCols = table[0].length;
String[][] transpose = new String[nCols][nRows];
// Do the transpose
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
transpose[j][i] = table[i][j];
}
}
return transpose;
}
If you want to go for in place transpose of a matrix(in which case row count = col count
) you can so following in Java
public static void inPlaceTranspose(int [][] matrix){
int rows = matrix.length;
int cols = matrix[0].length;
for(int i=0;i<rows;i++){
for(int j=i+1;j<cols;j++){
matrix[i][j] = matrix[i][j] + matrix[j][i];
matrix[j][i] = matrix[i][j] - matrix[j][i];
matrix[i][j] = matrix[i][j] - matrix[j][i];
}
}
}
public int[][] tiles, temp;
// Add values to tiles, wherever you end up doing that, then:
System.arraycopy(tiles, 0, temp, 0, tiles.length);
for(int row = 0; row < tiles.length; row++) // Loop over rows
for(int col = 0; col < tiles[row].length; col++) // Loop over columns
tiles[col][row] = temp[row][col]; // Rotate
That should do it for you.