Let a square matrix be:
mat={{1,2,3},{4,5,6},{0,0,0}};
Get a total for each column:
In: Total[mat]
Out: {5,7,9}
I would like to obtain the number of non-zero elements of each column (and the result to be in the form {a,b,c}).
Thanks
Let a square matrix be:
mat={{1,2,3},{4,5,6},{0,0,0}};
Get a total for each column:
In: Total[mat]
Out: {5,7,9}
I would like to obtain the number of non-zero elements of each column (and the result to be in the form {a,b,c}).
Thanks
Map[Length, Transpose[mat]]andConstantArray @@ Dimensions[mat]are two methods that come to mind. – MikeLimaOscar Oct 06 '17 at 12:21Transpose, i.e. if the matrix is not square -- without it you are giving the lengths of the rows. – MikeLimaOscar Oct 06 '17 at 12:35Count[#, Except[0]] & /@ Transpose[mat]gives{2, 2, 2}. If you might have inexact zeroes useExcept[0 | 0.]. – MikeLimaOscar Oct 06 '17 at 13:27