rray_diag: Create a matrix with 'x' on the diagonal

Description Usage Arguments Details Value Examples

View source: R/diagonal.R

Description

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

Usage

1
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.

Details

No dimension names will be on the result.

Value

A matrix, with x on the diagonal.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 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)

rray documentation built on July 23, 2019, 5:04 p.m.