Skip to main content

Command Palette

Search for a command to run...

Squaring a Rectangular Matrix

Published
•2 min read•View as Markdown
Squaring a Rectangular Matrix
A

Corporate trainer

Sometimes it is required to work with multi-dimensional arrays. However, we work mostly with two-dimensional arrays.

An array of two dimensions is a table data of rows and columns. It is typically referred to as MxN matrix, where M is rows and N is columns.

When rows and columns have the same length, it is a square matrix. However, we come across rectangular matrices and we may have to fill in the missing data to convert them to a square matrix.

Here's the routine to square a matrix that you might find useful. We are dealing with a matrix of numbers. The missing cells of this matrix are filled with a number of your choice.

public class Rect2SqMatrix {

    static int m = 0;
    static int n = 0;

    public static void main(String[] args) {

        int[][] matrix = getMatrix();
        showMatrix(matrix);
        boolean sq = isSquare(matrix);
        System.out.println(sq? "a square matrix" : "a rectangular matrix");

        System.out.println();

        // convert if reqd.
        if(!sq) {
            int[][] sqMatrix = rect2square(matrix, 1);
            showMatrix(sqMatrix);
            sq = isSquare(sqMatrix);
        System.out.println(sq? "a square matrix" : "a rectangular matrix");
        }

    }

    static boolean isSquare(int[][] mat) {
        int m = mat.length;
        int n = mat[0].length;
        return m*n == m*m;
    }

    static int[][] getMatrix() {
        // uncomment any one statement below to check.

        //int[][] matrix = {{1,2,3}, {2,3,4}, {4,5,6}};
        //int[][] matrix = {{1,2}, {2,3}, {3,4}};
        int[][] matrix = {{1,2,3}, {2,3,4}};

        return matrix;
    }

    static void showMatrix(int[][] matrix) {
        for(int i=0; i<matrix.length; i++) {
            for(int j=0; j<matrix[0].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    static int[][] rect2square(int[][] rectMatrix, int x) {

        int[][] sqMatrix = null;

        int m = rectMatrix.length;
        int n = rectMatrix[0].length;

        boolean greater = m > n;

        if(greater) {
            sqMatrix = new int[m][m];

            for(int i=0; i<m; i++) {
                for(int j=0; j<m; j++) {
                    sqMatrix[i][j] = x;
                }
            }
        } else {
            sqMatrix = new int[n][n];

            for(int i=0; i<n; i++) {
                for(int j=0; j<n; j++) {
                    sqMatrix[i][j] = x;
                }
            }
        }
        for(int i=0; i<m; i++) {
                for(int j=0; j<n; j++) {
                    sqMatrix[i][j] = rectMatrix[i][j];
                }
        }
        return sqMatrix;
    }

}

There is a routine that checks whether a given matrix is rectangular or a square.

As an exercise, the getMatrix() method may implement input from the user.