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.