Added MATLAB versions of min and max

This commit is contained in:
Waldir Leoncio 2020-10-19 13:11:52 +02:00
parent 3250349c60
commit dde6dd2e3a
2 changed files with 66 additions and 0 deletions

33
R/min_MATLAB.R Normal file
View file

@ -0,0 +1,33 @@
#' @title Minimum (MATLAB version)
#' @description Finds the minimum value for each column of a matrix, potentially returning the indices instead
#' @param X matrix
#' @param indices return indices?
#' @return Either a list or a vector
#' @author Waldir Leoncio
#' @export
min_MATLAB <- function(X, indices = TRUE) {
mins <- apply(X, 2, min)
idx <- sapply(seq_len(ncol(X)), function(x) match(mins[x], X[, x]))
if (indices) {
return(list(mins = mins, idx = idx))
} else {
return(mins)
}
}
#' @title Minimum (MATLAB version)
#' @description Finds the minimum value for each column of a matrix, potentially returning the indices instead
#' @param X matrix
#' @param indices return indices?
#' @return Either a list or a vector
#' @author Waldir Leoncio
#' @export
min_MATLAB <- function(X, indices = TRUE) {
mins <- apply(X, 2, min)
idx <- sapply(seq_len(ncol(X)), function(x) match(mins[x], X[, x]))
if (indices) {
return(list(mins = mins, idx = idx))
} else {
return(mins)
}
}

33
R/min_max_MATLAB.R Normal file
View file

@ -0,0 +1,33 @@
#' @title Minimum (MATLAB version)
#' @description Finds the minimum value for each column of a matrix, potentially returning the indices instead
#' @param X matrix
#' @param indices return indices?
#' @return Either a list or a vector
#' @author Waldir Leoncio
#' @export
min_MATLAB <- function(X, indices = TRUE) {
mins <- apply(X, 2, min)
idx <- sapply(seq_len(ncol(X)), function(x) match(mins[x], X[, x]))
if (indices) {
return(list(mins = mins, idx = idx))
} else {
return(mins)
}
}
#' @title Maximum (MATLAB version)
#' @description Finds the minimum value for each column of a matrix, potentially returning the indices instead
#' @param X matrix
#' @param indices return indices?
#' @return Either a list or a vector
#' @author Waldir Leoncio
#' @export
max_MATLAB <- function(X, indices = TRUE) {
maxs <- apply(X, 2, max)
idx <- sapply(seq_len(ncol(X)), function(x) match(maxs[x], X[, x]))
if (indices) {
return(list(maxs = maxs, idx = idx))
} else {
return(maxs)
}
}