Optimized squeeze()

This commit is contained in:
Waldir Leoncio 2020-07-14 11:18:01 +02:00
parent bb44d0cfd4
commit a27f303f4a
2 changed files with 7 additions and 10 deletions

View file

@ -6,17 +6,10 @@
#' a 3-by-1-by-1-by-2 array, then squeeze(A) returns a 3-by-2 matrix. If A is a #' a 3-by-1-by-1-by-2 array, then squeeze(A) returns a 3-by-2 matrix. If A is a
#' row vector, column vector, scalar, or an array with no dimensions of length #' row vector, column vector, scalar, or an array with no dimensions of length
#' 1, then squeeze returns the input A. #' 1, then squeeze returns the input A.
#' @note This is basically a wrapper of drop() with a minor adjustment to adapt
#' the output to what happens on Matlab
#' @param A input or array matrix #' @param A input or array matrix
#' @return An array with the same elements as the input array, but with #' @return An array with the same elements as the input array, but with
#' dimensions of length 1 removed. #' dimensions of length 1 removed.
#' @author Waldir Leoncio #' @author Waldir Leoncio
squeeze <- function(A) { squeeze <- function(A) as.matrix(drop(A))
A <- as.array(A)
dim_1 <- which(dim(A) == 1)
B <- array(A, dim = dim(A)[-dim_1])
# Workaround to match Matlab behavior
if (length(dim(B)) == 1) B <- as.matrix(B)
return(B)
}

View file

@ -24,6 +24,10 @@ a 3-by-1-by-1-by-2 array, then squeeze(A) returns a 3-by-2 matrix. If A is a
row vector, column vector, scalar, or an array with no dimensions of length row vector, column vector, scalar, or an array with no dimensions of length
1, then squeeze returns the input A. 1, then squeeze returns the input A.
} }
\note{
This is basically a wrapper of drop() with a minor adjustment to adapt
the output to what happens on Matlab
}
\author{ \author{
Waldir Leoncio Waldir Leoncio
} }