Skip to main content

counting - Count Column Elements in a Matrix




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



Answer



I think the simplest version is:


Total @ Unitize @ mat



{2, 2, 2}



Comments