https://github.com/cran/pracma
Tip revision: 03698027c2d84118bd0c53c4a9a5b5d23676f388 authored by HwB on 01 October 2012, 00:00:00 UTC
version 1.2.0
version 1.2.0
Tip revision: 0369802
size.R
##
## s i z e . R Matlab size, numel, ndims, and isempty functions
##
size <- function(x, k) {
if (length(x) == 0) sz <- 0
else if (is.vector(x)) sz <- c(1, length(x))
else if (is.array(x)) sz <- dim(x)
else sz <- NULL
if (!missing(k)) {
if (k > length(sz)) sz <- 1
else if (k >= 1) sz <- sz[k]
else
stop("Requested dimension 'k' is out of range.")
}
return(sz)
}
numel <- function(x) {
sz <- size(x)
if (!is.null(sz)) prod(sz)
else return(NULL)
}
nnz <- function(x) {
if (length(x) == 0) return(0)
stopifnot(is.numeric(x) || is.complex(x))
sum(x != 0)
}
ndims <- function(x) {
sz <- size(x)
if (!is.null(sz)) length(sz)
else return(NULL)
}
isempty <- function(x) {
length(x) == 0
}