Learn R Programming

rray (version 0.1.0)

rray_diag: Create a matrix with x on the diagonal

Description

rray_diag() creates a matrix filled with x on the diagonal. Use offset to place x along an offset from the diagonal.

Usage

rray_diag(x, offset = 0)

Arguments

x

A vector, matrix, array or rray.

offset

A single integer specifying the offset from the diagonal to place x. This can be positive or negative.

Value

A matrix, with x on the diagonal.

Details

No dimension names will be on the result.

Examples

Run this code
# NOT RUN {
# Creates a diagonal matrix
rray_diag(1:5)

# Offset `1:5` by 1
rray_diag(1:5, 1)

# You can also go the other way
rray_diag(1:5, -1)

# Identity matrix
rray_diag(rep(1, 5))

# One interesting use case of this is to create
# a square empty matrix with dimensions (offset, offset)
rray_diag(rray(integer()), 3)
rray_diag(logical(), 3)

# }

Run the code above in your browser using DataLab