
Define function to create a 'diagonal' array or get the diagonal of an array
Source:R/diag3Darray.R
diag3Darray.RdDefine function to create a 'diagonal' array or get the diagonal of an array
Usage
diag3Darray(x = 1, dim = length(x), out = "vector")Arguments
- x
numberorvectordefining the value of the diagonal of3D array- dim
integerdefining the length of the diagonal. Default islength(x). Iflength(x) != 1,dimmust be equal tolength(x).- out
characterspecifying which type of diagonal to return ("vector"or"matrix"). SeeDetails
Details
The diagonal of a 3D array has been defined as those elements in positions c(int,int,int), i.e., the three digits are the same.
If the diagonal should be returned, out specifies if it should return a "vector" with
the elements of position c(int,int,int), or "matrix" with the elements of position c(int,dim,int),
i.e., dim = 2 -> elements (1,1,1),(2,1,2),(3,1,3),(1,2,1),(2,2,2),(3,2,3),(3,1,3),(3,2,3),(3,3,3).
Examples
x <- diag3Darray(c(1,4,6), dim = 3)
x
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 0 0
#> [2,] 0 0 0
#> [3,] 0 0 0
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 0 4 0
#> [3,] 0 0 0
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 0 0 0
#> [3,] 0 0 6
#>
# , , 1
#
# [,1] [,2] [,3]
# [1,] 1 0 0
# [2,] 0 0 0
# [3,] 0 0 0
#
# , , 2
#
# [,1] [,2] [,3]
# [1,] 0 0 0
# [2,] 0 4 0
# [3,] 0 0 0
#
# , , 3
#
# [,1] [,2] [,3]
# [1,] 0 0 0
# [2,] 0 0 0
# [3,] 0 0 6
diag3Darray(x)
#> [1] 1 4 6
# 1, 4, 6