0

I have found a way to delete rows and columns from a 2D double matrix using Apache Commons Math. My condition for deletion was if a column or row had all zeroes. Is there a similar way for integer matrix? I want to avoid creating copies of large double matrices.

public double[][] removeRowsAndColumnsWithAllZeroes(double[][] data)
{
    //double[][] data =
    //  {
    //          {1, 0, 0},
    //          {1, 1, 0},
    //          {1, 1, 0},
    //          {0, 1, 0},
    //          {0, 0, 0}};

    RealMatrix m = MatrixUtils.createRealMatrix(data);
    List<Integer> rowList = new ArrayList<Integer>();
    List<Integer> colList = new ArrayList<Integer>();

    for (int i = 0; i < m.getRowDimension(); i++)
    {
        double total = 0;
        for (double r : m.getRow(i))
           total += r;
        if (total == 0)
            continue;
        rowList.add(i);
    }

    for (int i = 0; i < m.getColumnDimension(); i++)
    {
        double total = 0;
        for (double r : m.getColumn(i))
            total += r;
        if (total == 0)
            continue;
        colList.add(i);
    }

    int[] columns = new int[colList.size()];
    int[] rows = new int[rowList.size()];

    for (int i = 0; i < columns.length; i++)
        columns[i] = colList.get(i);
    for (int i = 0; i < rows.length; i++)
        rows[i] = rowList.get(i);

    System.out.println(m);
    //Array2DRowRealMatrix{{1.0,0.0,0.0},{1.0,1.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,0.0}}

    System.out.println("Rows:\t" + m.getRowDimension() + "\tColumns:\t" +  m.getColumnDimension());
    // Rows:    5   Columns:    3

    RealMatrix n = m.getSubMatrix(rows, columns);

    System.out.println(n);
                // Array2DRowRealMatrix{{1.0,0.0},{1.0,1.0},{1.0,1.0},{0.0,1.0}}

    System.out.println("Rows:\t" + n.getRowDimension() + "\tColumns:\t" +  n.getColumnDimension());
    // Rows:    4   Columns:    2

    return n.getData();

}
Nikhil
  • 1,126
  • 12
  • 26

1 Answers1

0

as an example, below code identifies the Rows and Columns having all zeroes. a 4x3 array is taken as input. Note an ArrayList can be converted to an Array and reverse.

public class ZeroesIn2Darray{

static int dataArr[][] = {{1,0,0},
                          {0,0,0},   
                          {1,0,0},
                          {0,0,0}};

public static void main(String[] args){
int rowCount = dataArr.length;
int colCount = dataArr[0].length;

int[] rowsForDeletion = new int[rowCount];   //stores identified row#s
int indexRowsForDeletion = 0;
int[] colsForDeletion = new int[colCount];   //stores identified col#s
int indexColsForDeletion = 0;

// identify rows with all 0s
for(int i=0; i<rowCount; i++){
 int rowTotal = 0;
 for(int j=0; j<colCount; j++)
  rowTotal += dataArr[i][j];            //gets sum for row=i

 if (rowTotal == 0)
  rowsForDeletion[indexRowsForDeletion++] = i;
}

// print the identified rows
System.out.print("Rows with all zeroes: ");
for (int k=0;k<indexRowsForDeletion;k++)
 System.out.print(rowsForDeletion[k] + " ");
System.out.println();

// identify cols with all 0s
for(int j=0; j<colCount; j++){
 int colTotal = 0;
 for(int i=0; i<rowCount; i++)
  colTotal += dataArr[i][j];            //gets sum for col=j

 if (colTotal == 0)
  colsForDeletion[indexColsForDeletion++] = j;
}

// print the identified cols
System.out.print("Cols with all zeroes: ");
for (int l=0;l<indexColsForDeletion;l++)
 System.out.print(colsForDeletion[l] + " ");
System.out.println();

 }         //end of main()
}          //end of class
Saurabh
  • 23
  • 4