ourMELONS/R/times.R

51 lines
1.7 KiB
R
Raw Normal View History

2020-01-14 16:36:00 +01:00
#' @title Element-wise matrix multiplication
#' @description Emulates the `times()` and `.*` operators from Matlab.
#' @details This function basically handles elements of different length better than the `*` operator in R, at least as far as behavior from a Matlab user is expecting.
#' @param a first factor of the multiplication
#' @param b second factor of the multiplication
#' @export
#' @returns matrix with dimensions equal to the larger of the two factors
times <- function(a, b) {
# Converting everything to matrix because Matlab looooooves the matrix
a <- as.matrix(a)
b <- as.matrix(b)
dominant_mx <- NULL
if (!all(dim(a) == dim(b))) {
if (all(dim(a) >= dim(b))) {
dominant_mx <- a
dominated_mx <- b
} else if (all(dim(b) >= dim(a))) {
dominant_mx <- b
dominated_mx <- a
2020-01-15 13:20:23 +01:00
} else {
dominant_mx <- "neither"
dominant_dim <- c(max(nrow(b), nrow(a)), max(ncol(b), ncol(a)))
2020-01-14 16:36:00 +01:00
}
}
if (is.null(dominant_mx)) {
2020-01-15 13:20:23 +01:00
out <- a * b
2020-01-15 13:29:47 +01:00
} else if (dominant_mx[1] == "neither") {
2020-01-15 13:20:23 +01:00
a <- repmat(
mx = a,
n = c(dominant_dim[1] - nrow(a) + 1, dominant_dim[2] - ncol(a) + 1)
)
b <- repmat(
mx = b,
n = c(dominant_dim[1] - nrow(b) + 1, dominant_dim[2] - ncol(b) + 1)
)
out <- a * b
2020-01-14 16:36:00 +01:00
} else {
# Expanding dominated matrix
dominated_mx <- repmat(
mx = dominated_mx,
n = c(
nrow(dominant_mx) - nrow(dominated_mx) + 1,
ncol(dominant_mx) - ncol(dominated_mx) + 1
)
)
2020-01-15 13:20:23 +01:00
out <- dominant_mx * dominated_mx
2020-01-14 16:36:00 +01:00
}
2020-01-15 13:20:23 +01:00
return(out)
2020-01-14 16:36:00 +01:00
}