Fixed bugs in size() for non-sequential x

This commit is contained in:
Waldir Leoncio 2020-03-03 14:31:15 +01:00
parent 8ecc59a4e6
commit 11bdefab7c

View file

@ -2,7 +2,7 @@
#' @description This functions tries to replicate the behavior of the base function "size" in Matlab
#' @param x object to be evaluated
#' @param d dimension of object to be evaluated
#' @note On MATLAB, size(1, 100) returns 1. As a matter of fact, if the user
#' @note On MATLAB, size(1, 100) returns 1. As a matter of fact, if the user
#' calls for a dimension which x doesn't have `size()` always returns 1. R's
#' default behavior is more reasonable in those cases (i.e., returning NA),
#' but since the point of this function is to replicate MATLAB behaviors
@ -25,12 +25,12 @@ size <- function(x, d) {
n_dim <- ifelse(is.null(dim(x)), 1, length(dim(x)))
if (missing(d)) {
if (n_dim == 1) {
out <- range(x)
out <- c(1, length(x))
} else {
out <- dim(x)
}
} else {
out <- ifelse(n_dim == 1, range(x)[d], dim(x)[d])
out <- ifelse(n_dim == 1, c(1, length(x))[d], dim(x)[d])
if (is.na(out)) out <- 1 # for MATLAB compatibility
}
return(out)