diff --git a/R/zeros_ones.R b/R/zeros_ones.R new file mode 100644 index 0000000..2bc6f00 --- /dev/null +++ b/R/zeros_ones.R @@ -0,0 +1,31 @@ +#' @title Matrix of zeros or ones +#' @description Generates a square or rectangular matrix of zeros or ones +#' @param n scalar or 2D vector +#' @param x value to fill matrix with +#' @return n-by-n matrix filled with `x` +#' @details This is a wrapper function to replicate the behavior of the +#' `zeros()` and the `ones()` functions on Matlab +#' @note Actually works for any `x`, but there's no need to bother imposing +#' validation controls here. +zeros_or_ones <- function(n, x) { + if (length(n) == 1) n <- c(n, n) + return(matrix(x, n[1], n[2])) +} + +#' @title Matrix of zeros +#' @description wrapper of `zeros_or_ones()` that replicates the behavior of +#' the `zeros()` function on Matlab +#' @param n1 number of rows +#' @param n2 number of columns +zeros <- function(n1, n2 = n1) { + return(zeros_or_ones(c(n1, n2), 0)) +} + +#' @title Matrix of ones +#' @description wrapper of `zeros_or_ones()` that replicates the behavior of +#' the `ones()` function on Matlab +#' @param n1 number of rows +#' @param n2 number of columns +ones <- function(n1, n2 = n1) { + return(zeros_or_ones(c(n1, n2), 1)) +} \ No newline at end of file diff --git a/man/ones.Rd b/man/ones.Rd new file mode 100644 index 0000000..e739460 --- /dev/null +++ b/man/ones.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/zeros_ones.R +\name{ones} +\alias{ones} +\title{Matrix of ones} +\usage{ +ones(n1, n2 = n1) +} +\arguments{ +\item{n1}{number of rows} + +\item{n2}{number of columns} +} +\description{ +wrapper of `zeros_or_ones()` that replicates the behavior of +the `ones()` function on Matlab +} diff --git a/man/zeros.Rd b/man/zeros.Rd new file mode 100644 index 0000000..039b95b --- /dev/null +++ b/man/zeros.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/zeros_ones.R +\name{zeros} +\alias{zeros} +\title{Matrix of zeros} +\usage{ +zeros(n1, n2 = n1) +} +\arguments{ +\item{n1}{number of rows} + +\item{n2}{number of columns} +} +\description{ +wrapper of `zeros_or_ones()` that replicates the behavior of +the `zeros()` function on Matlab +} diff --git a/man/zeros_or_ones.Rd b/man/zeros_or_ones.Rd new file mode 100644 index 0000000..a9ec7f7 --- /dev/null +++ b/man/zeros_or_ones.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/zeros_ones.R +\name{zeros_or_ones} +\alias{zeros_or_ones} +\title{Matrix of zeros or ones} +\usage{ +zeros_or_ones(n, x) +} +\arguments{ +\item{n}{scalar or 2D vector} + +\item{x}{value to fill matrix with} +} +\value{ +n-by-n matrix filled with `x` +} +\description{ +Generates a square or rectangular matrix of zeros or ones +} +\details{ +This is a wrapper function to replicate the behavior of the +`zeros()` and the `ones()` functions on Matlab +} +\note{ +Actually works for any `x`, but there's no need to bother imposing +validation controls here. +}