From 240a3705e4bcd2c39cebd6122cb9efdecb2af87e Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Tue, 25 Feb 2020 11:48:12 +0100 Subject: [PATCH] Added reshape function --- R/reshape.R | 24 ++++++++++++++++++++++++ man/reshape.Rd | 27 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 R/reshape.R create mode 100644 man/reshape.Rd diff --git a/R/reshape.R b/R/reshape.R new file mode 100644 index 0000000..824bbae --- /dev/null +++ b/R/reshape.R @@ -0,0 +1,24 @@ +#' @title Reshape array +#' @description Reshapes a matrix according to a certain number of dimensions +#' @param A input matrix +#' @param sz vector containing the dimensions of the output vector +#' @details This function replicates the functionality of the `reshape()` +#' function on Matlab. This function is basically a fancy wrapper for the +#' `array()` function in R, but is useful because it saves the user translation +#' time. Moreover, it introduces validation code that alter the behavior of +#' `array()` and makes it more similar to `replicate()`. +#' @note The Matlab function also accepts as input the dismemberment of sz as +#' scalars. +reshape <- function(A, sz) { + # Validation + if (prod(sz) != prod(dim(A))) { + stop("To RESHAPE the number of elements must not change.") + } + if (length(sz) == 1) { + stop("Size vector must have at least two elements.") + } + + # Reshaping A + A <- array(A, sz) + return(A) +} \ No newline at end of file diff --git a/man/reshape.Rd b/man/reshape.Rd new file mode 100644 index 0000000..39d74f9 --- /dev/null +++ b/man/reshape.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/reshape.R +\name{reshape} +\alias{reshape} +\title{Reshape array} +\usage{ +reshape(A, sz) +} +\arguments{ +\item{A}{input matrix} + +\item{sz}{vector containing the dimensions of the output vector} +} +\description{ +Reshapes a matrix according to a certain number of dimensions +} +\details{ +This function replicates the functionality of the `reshape()` +function on Matlab. This function is basically a fancy wrapper for the +`array()` function in R, but is useful because it saves the user translation +time. Moreover, it introduces validation code that alter the behavior of +`array()` and makes it more similar to `replicate()`. +} +\note{ +The Matlab function also accepts as input the dismemberment of sz as +scalars. +}