ourMELONS/R/cell.R

36 lines
1.1 KiB
R
Raw Normal View History

2020-06-24 16:07:13 +02:00
#' @title Cell array
#' @description Creates an array of zeros
#' @param n a the first dimension (or both, if sz is not passed)
#' @param sz the second dimension (or 1st and 2nd, if not passed)
2021-03-31 10:26:56 +02:00
#' @param expandable if TRUE, output is a list (so it can take different
#' lengths)
2020-06-24 16:49:31 +02:00
#' @param ... Other dimensions
2020-06-24 16:07:13 +02:00
#' @return An array of zeroes with the dimensions passed on call
2021-03-31 10:26:56 +02:00
cell <- function(n, sz = c(n, n), expandable=FALSE, ...) {
# Uglyly figuring out if the third arg is an extra dim --- #
sz3 <- vector()
if (!is.logical(expandable)) {
sz3 <- expandable
expandable <- FALSE
}
args <- c(as.list(environment()), list(...))
exp <- args$expandable
extra_dims <- c(sz3, args[names(args) == ""])
# Creating output vector --------------------------------- #
if (exp) {
2021-03-31 10:26:56 +02:00
return(vector("list", length = n))
}
if (length(sz) == 1 & length(extra_dims) == 0) {
2020-07-28 15:35:29 +02:00
return(array(0, dim = c(n, sz)))
} else if (length(extra_dims) > 0) {
return(array(0, dim = c(n, sz, extra_dims)))
2020-06-24 16:07:13 +02:00
} else if (length(sz) == 2) {
2020-07-28 15:35:29 +02:00
return(array(0, dim = sz))
2020-06-24 16:07:13 +02:00
} else {
2020-07-28 15:35:29 +02:00
return(array(0, dim = c(n, sz, ...)))
2020-06-24 16:07:13 +02:00
}
}