Transposing a matrix from a 2D array

后端 未结 5 2110
借酒劲吻你
借酒劲吻你 2021-01-06 19:38

I\'m self teaching myself some java and I\'m stuck on creating a 2D array that initializes it with random values and then creates the transpose of the array.

An ex

相关标签:
5条回答
  • 2021-01-06 20:16

    For a square matrix, instead of iterating through the entire array, you just iterate through the diagonally half of the 2D array and swap the values with the corresponding indices.

    public void transposeMatrix(int[][] a) {
            for(int i=0 ; i<n; i++) { 
                for(int j=0 ; j<i ; j++) {
                    int temp = a[i][j];
                    a[i][j] = a[j][i];
                    a[j][i] = temp;
                }
            }
    }
    
    0 讨论(0)
  • 2021-01-06 20:23

    This is a simple method that return an int[][] of the transposed matrix...

    public static int[][] transposeMatrix(int[][] matrix){
        int m = matrix.length;
        int n = matrix[0].length;
    
        int[][] transposedMatrix = new int[n][m];
    
        for(int x = 0; x < n; x++) {
            for(int y = 0; y < m; y++) {
                transposedMatrix[x][y] = matrix[y][x];
            }
        }
    
        return transposedMatrix;
    }
    

    Than to print a 2D matrix you can use a method like this:

    public static String matrixToString(int[][] a){
        int m = a.length;
        int n = a[0].length;
    
        String tmp = "";
        for(int y = 0; y<m; y++){
            for(int x = 0; x<n; x++){
                tmp = tmp + a[y][x] + " ";
            }
            tmp = tmp + "\n";
        }
    
        return tmp;
    }
    
    0 讨论(0)
  • 2021-01-06 20:24

    The answer provided above is not efficient in terms of memory. It is using another array - transposedMatrix apart from array supplied as argument. This will lead to consume double memory. We can do this in-place as follows:

    public void transposeMatrix(int[][] a)
    {
            int temp;
            for(int i=0 ; i<(a.length/2 + 1); i++)
            {
                for(int j=i ; j<(a[0].length) ; j++)
                {
                    temp = a[i][j];
                    a[i][j] = a[j][i];
                    a[j][i] = temp;
                }
            }
    
            displayMatrix(a);
        }
    
    public void displayMatrix(int[][] a){
            for(int i=0 ; i<a.length ; i++)
            {
                for(int j=0 ; j<a[0].length ; j++)
                {
                    System.out.print(a[i][j] + " ");
                }
    
                System.out.println();
            }
        }   
    
    0 讨论(0)
  • 2021-01-06 20:30

    You can use the below class it has most of the methods you want.

    /**
     * Class representing square matrix of given size.
     * It has methods to rotate by 90, 180 and 270
     * And also to transpose and flip on either axis.
     * 
     * I have used both space efficient methods in transpose and flip
     * And simple but more space usage for rotation. 
     * 
     * This is using builder pattern hence, you can keep on applying
     * methods say rotate90().rotate90() to get 180 turn.
     * 
     */
    public class Matrix {
    
        private int[][] matrix;
        final int size;
    
        public Matrix(final int size) {
            this.size = size;
            matrix = new int[size][size];
    
            for (int i=0;i<size;i++)
                for (int j=0;j<size;j++)
                    matrix[i][j] = i*size + j;
        }
    
        public Matrix rotate90() {
            int[][] temp = new int[size][size];
    
            for (int i=0;i<size;i++)
                for (int j=0;j<size;j++)
                    temp[i][j] = matrix[size-1-j][i];
    
            matrix = temp;
            return this;
        }
        public Matrix rotate180() {
            int[][] temp = new int[size][size];
    
            for (int i=0;i<size;i++)
                for (int j=0;j<size;j++)
                    temp[i][j] = matrix[size-1-i][size-1-j];
    
            matrix = temp;
            return this;
        }
        public Matrix rotate270() {
            int[][] temp = new int[size][size];
    
            for (int i=0;i<size;i++)
                for (int j=0;j<size;j++)
                    temp[i][j] = matrix[j][size-1-i];
    
            matrix = temp;
            return this;
        }
        public Matrix transpose() {
            for (int i=0; i<size-1; i++) {
                for (int j=i+1; j<size; j++) {
                    int tmp = matrix[i][j];
                    matrix[i][j] = matrix[j][i];
                    matrix[j][i] = tmp;
                }
            }
            return this;
        }
        public Matrix flipVertical() {
            for (int i=0; i<size; i++) {
                for (int j=0; j<size/2; j++) {
                    int tmp = matrix[i][size-1-j];
                    matrix[i][size-1-j] = matrix[i][j];
                    matrix[i][j] = tmp;
                }
            }
            return this;
        }
        public Matrix flipHorizontal() {
            for (int i=0; i<size/2; i++) {
                for (int j=0; j<size; j++) {
                    int tmp = matrix[size-1-i][j];
                    matrix[size-1-i][j] = matrix[i][j];
                    matrix[i][j] = tmp;
                }
            }
            return this;
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            for (int i=0;i<size;i++) {
                for (int j=0;j<size;j++) {
                    sb.append("|");
                    sb.append(matrix[i][j]);
                    if (size > 3) {
                        sb.append("\t");
                    }
                }
                sb.append("|\n");
            }
    
            return sb.toString();
        }
    
        public static void main(String... args) {
            Matrix m = new Matrix(3);
            System.out.println(m);
    
            //transpose and flipHorizontal is 270 turn (-90)
            System.out.println(m.transpose());
            System.out.println(m.flipHorizontal());
    
            //rotate 90 further to bring it back to original position
            System.out.println(m.rotate90());
    
            //transpose and flip Vertical is 90 degree turn
            System.out.println(m.transpose().flipVertical());
        }
    }
    

    Output:

    |0|1|2|
    |3|4|5|
    |6|7|8|
    
    |0|3|6|
    |1|4|7|
    |2|5|8|
    
    |2|5|8|
    |1|4|7|
    |0|3|6|
    
    |0|1|2|
    |3|4|5|
    |6|7|8|
    
    |6|3|0|
    |7|4|1|
    |8|5|2|
    
    0 讨论(0)
  • 2021-01-06 20:34

    Here is Kotlin Solution!

    fun displayMatrix(matrix: Array<IntArray>) {
        for(row in matrix) {
            for(column in row) {
                print("$column  ")
            }
            println()
        }
    }
    
    fun main() {
    
    //Create Array
    val matrixA = arrayOf(intArrayOf(2, 3, 4), intArrayOf(5, 6, 4), intArrayOf(1, 6, 7), intArrayOf(0, 2, 8))
    
    println("Dimension on X: ${matrixA[0].size}")
    println("Dimension on Y: ${matrixA.size}")
    
    displayMatrix(matrixA)
    
    println()
    
    val transposeMatrix = Array(matrixA[0].size) { IntArray(matrixA.size) }
    
    
    for(i in 0..matrixA.size - 1) {
        for(j in 0..matrixA[0].size - 1) {
            transposeMatrix[j][i] = matrixA[i][j]
        }
    }
    
    displayMatrix(transposeMatrix)
    
    
    }
    
    0 讨论(0)
提交回复
热议问题