Extract or replace the antidiagonal of a matrix, or construct a antidiagonal matrix.

antidiag(x = as.numeric(c(1)), nrow = NULL, ncol = NULL)

antidiag(x) <- value

Arguments

x

matrix, vector or 1D array, or missing.

nrow

number of rows (optional; when x is not a matrix)

ncol

number of columns (optional; when x is not a matrix)

value

either a single value or a vector of length equal to that of the current antidiagonal. Should be of a mode which can be coerced to that of x.

Examples

# Extracting antidiag antidiag(diag(3))
#> [1] 0 1 0
# Creating antidiagonal matrix antidiag(7, 3, 3)
#> [,1] [,2] [,3] #> [1,] 0 0 7 #> [2,] 0 7 0 #> [3,] 7 0 0
antidiag(1:5, 3, 3)
#> [,1] [,2] [,3] #> [1,] 0 0 1 #> [2,] 0 2 0 #> [3,] 3 0 0
# Assigning antidiagonal mat <- matrix(0, 3, 3) antidiag(mat) <- c(3, 4, 5) mat
#> [,1] [,2] [,3] #> [1,] 0 0 3 #> [2,] 0 4 0 #> [3,] 5 0 0