
Last chance! 50% off unlimited learning
Sale ends in
x
inheriting from class
Matrix
.nnzero(x, na.counted = NA)
Matrix
or numeric
.x
(typically
integer
). Note that for a symmetric sparse matrix S
(i.e., inheriting from
class
), nnzero(S)
is
typically twice the length(S@x)
.
Matrix
class also has a
length
method; typically, length(M)
is much
larger than nnzero(M)
for a sparse matrix M, and the latter is
a better indication of the size of M
.m <- Matrix(0+1:28, nrow = 4)
m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0
(mT <- as(m, "dgTMatrix"))
nnzero(mT)
(S <- crossprod(mT))
nnzero(S)
str(S) # slots are smaller than nnzero()
stopifnot(nnzero(S) == sum(as.matrix(S) != 0))# failed earlier
data(KNex)
M <- KNex$mm
class(M)
dim(M)
length(M); stopifnot(length(M) == prod(dim(M)))
nnzero(M) # more relevant than length
## the above are also visible from
str(M)
Run the code above in your browser using DataLab