Added R equivalents to zeros() and ones()
This commit is contained in:
parent
220c81a237
commit
98affbfc1c
4 changed files with 92 additions and 0 deletions
31
R/zeros_ones.R
Normal file
31
R/zeros_ones.R
Normal file
|
|
@ -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))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue