Skip to contents

Repeats a matrix over n columns and rows

Usage

repmat(mx, n)

Arguments

mx

matrix

n

either a scalar with the number of replications in both rows and columns or a <= 3-length vector with individual repetitions.

Value

matrix replicated over ncol(mx) * n columns and nrow(mx) * n rows

Details

This function was created to replicate the behavior of a homonymous function on Matlab

Note

The Matlab implementation of this function accepts n with length > 2.

It should also be noted that a concatenated vector in R, e.g. c(5, 2), becomes a column vector when coerced to matrix, even though it may look like a row vector at first glance. This is important to keep in mind when considering the expected output of this function. Vectors in R make sense to be seen as column vectors, given R's Statistics-oriented paradigm where variables are usually disposed as columns in a dataset.

Examples

x <- matrix(1:4, 2)
repmat(x, 1)
#>      [,1] [,2]
#> [1,]    1    3
#> [2,]    2    4
repmat(x, 2)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    3    1    3
#> [2,]    2    4    2    4
#> [3,]    1    3    1    3
#> [4,]    2    4    2    4
repmat(x, c(2, 3))
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    1    3    1    3    1    3
#> [2,]    2    4    2    4    2    4
#> [3,]    1    3    1    3    1    3
#> [4,]    2    4    2    4    2    4