Create a matrix with ones on the diagonal and zeros elsewhere
stm_eye.Rd
stm_eye()
creates a matrix with ones along the diagonal, allowing
for an offset of the diagonal (parameter k
).
Arguments
- nrows
An integer specifying the number of rows in the matrix. Must be greater than or equal to zero.
- ncols
An integer specifying the number of columns in the matrix. Defaults to
nrows
. Must be greater than or equal to zero.- k
Optional. An integer specifying the diagonal offset. The default is
0
for the main diagonal. A positive value retrieves diagonals above the main diagonal, while a negative value retrieves diagonals below the main diagonal.
Value
A matrix of dimensions nrows
x ncols
with ones along
the diagonal (or at the offset specified by k
), and zeros
elsewhere.
Details
The function generates a matrix where ones appear on the diagonal (or an
offset diagonal determined by k
), and zeros appear everywhere else.
If k
exceeds the valid range (i.e., the diagonal index is out of
bounds), a matrix of zeros is returned.
Examples
stm_eye(nrows = 3)
#> [,1] [,2] [,3]
#> [1,] 1 0 0
#> [2,] 0 1 0
#> [3,] 0 0 1
stm_eye(nrows = 4, k = 1)
#> [,1] [,2] [,3] [,4]
#> [1,] 0 1 0 0
#> [2,] 0 0 1 0
#> [3,] 0 0 0 1
#> [4,] 0 0 0 0
stm_eye(nrows = 4, ncols = 5, k = -2)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0 0 0 0 0
#> [2,] 0 0 0 0 0
#> [3,] 1 0 0 0 0
#> [4,] 0 1 0 0 0