https://github.com/cran/pracma
Raw File
Tip revision: 392ae21a013fb3f518e8f9eb8efb458a55a2eca2 authored by HwB on 09 April 2011, 00:00:00 UTC
version 0.3-0
Tip revision: 392ae21
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)
}

ndims <- function(x) {
	sz <- size(x)
	if (!is.null(sz)) length(sz)
	else              return(NULL)
}

isempty <- function(x) {
	length(x) == 0
}
back to top