Search code examples
matlabmatrixsparse-matrix

Find sparsest row in matrix


I have a matrix in MATLAB, and I want to determine which row contains the most zeros. That is, I want to find the sparsest row (and column) in the matrix. Any way to do this more efficiently than looping with mat(sum(mat==0,i)==i,:)? Or is this the preferred method?

I'm using this for an implementation of an LU-factorization by using "minimum-degree ordering heuristic" to solve a linear system.


Solution

  • To find the "sparsest" row if I'm interpreting this correctly, that means you want to find the row with the most number of zeroes. You can use sum in a vectorized fashion combined with max to figure that out:

    [~, row] = max(sum(mat == 0, 2));
    

    mat == 0 will convert your entire matrix into true/false such that true is a zero value and false otherwise, then we use sum and sum over each row individually. This will reduce the problem such that each element counts up how many zero elements we encountered per row. Using the second output of max on this result, row will thus contain whichever row had the largest amount of zeroes. We ignore the first output as this will output the actual largest value which we don't care about given your problem.

    If you're concerned about speed, you can perform a matrix-vector multiplication with the converted true/false matrix with a vector of ones. This way it will perform the sum for you and thus the matrix gets promoted to double precision:

    [~, row] = max((mat == 0)*ones(size(mat, 2), 1)));
    

    Minor Note

    Take note that if there are multiple rows that share the same number of the largest number of zeroes found, the limitation with this approach is that it will return the first row that satisfies this condition. In terms of your objective, I believe this should be sufficient. Also be aware that if your matrix contains no zeroes, the output of this approach will default to the first row. There are some corner cases that I didn't take into account, but I'm not sure to what extent you would need to check for these so I'll leave those out for the sake of simplicity.