From dde6dd2e3ac35a50aa37ae0836903758eb16202b Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Mon, 19 Oct 2020 13:11:52 +0200 Subject: [PATCH] Added MATLAB versions of min and max --- R/min_MATLAB.R | 33 +++++++++++++++++++++++++++++++++ R/min_max_MATLAB.R | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 R/min_MATLAB.R create mode 100644 R/min_max_MATLAB.R diff --git a/R/min_MATLAB.R b/R/min_MATLAB.R new file mode 100644 index 0000000..af91efd --- /dev/null +++ b/R/min_MATLAB.R @@ -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) + } +} \ No newline at end of file diff --git a/R/min_max_MATLAB.R b/R/min_max_MATLAB.R new file mode 100644 index 0000000..8f5069f --- /dev/null +++ b/R/min_max_MATLAB.R @@ -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) + } +} \ No newline at end of file