Check if rows and columns of matrices have more than one non-zero element?
ClearAll[f]
f[mat_?(MatrixQ[#, NumericQ]&)] :=
FreeQ[
Through[{Total, Map[Total]} @ Unitize[mat]],
_?(# > 1 &)
]
The code uses Unitize
to turn any non-zero number into 1. Total
sums the columns of the unitized matrix; Map[Total]
sums the rows. The sums are therefore counts of non-zero elements in each column vs. row. FreeQ checks whether any of those counts is higher than 1, which indicates that there was more than one non-zero element in that column vs. row.
Let's make some matrices to test:
SeedRandom[4563]
MatrixForm /@
(list =
RandomChoice[{200, 10, 10, 10} -> {0, 1, 2, 3}, {10, 5, 5}])
and apply our test function f
to each:
f /@ list
(* Out: {False, True, False, False, False, False, False, True, False, True} *)
Following the comment and edit mentioning the presence of symbols rather than numbers, here is an alternative function:
ClearAll[fSym]
fSym[mat_] :=
FreeQ[
Through[{Total, Map[Total]}@mat],
_Plus,
-1
]
Same test, but with symbolic variables:
SeedRandom[4563]
MatrixForm /@ (listSym =
RandomChoice[{200, 10, 10, 10} -> {0, a, b, c}, {10, 5, 5}])
fSym /@ listSym
(* Out: {False, True, False, False, False, False, False, True, False, True} *)
m = {{0, a, 0}, {b, 0, 0}, {0, 0, c}};
f[m_] := AllTrue[
Count[#, 0] & /@ Join[m, Transpose[m]], # >= Length@m - 1 &]
True
Original
f[m_] := Nor @@ (AllTrue[#, EqualTo[0]] & /@ Join[m, Transpose[m]]);
m = {{0, 0, 1}, {1, 0, 1}, {2, 0, 0}};
n = {{1, 1, 1}, {0, 0, 1}, {0, 1, 0}};
f[m]
f[n]
False
True