Skip to content
Prev 263779 / 398502 Next

columnwise nnzero for dgCMatrix

Hi:

What about

colSums(mymat != 0)

?

# Example:
x <- matrix(sample(c(-1, 0, 1, 2), 100, replace = TRUE, prob = c(0.1,
0.8, 0.07, 0.03)),
              nrow = 25)
which(x != 0)  # 17 nonzero elements in this matrix
 [1]  9 17 27 32 36 41 44 45 49 50 62 67 68 72 76 78 82
colSums(x != 0)
[1] 2 8 4 3


Dennis
On Sun, Jun 26, 2011 at 3:15 AM, C6H5NO2 <C6H5NO2 at gmail.com> wrote: