Skip to contents

Define function to create a 'diagonal' array or get the diagonal of an array

Usage

diag3Darray(x = 1, dim = length(x), out = "vector")

Arguments

x

number or vector defining the value of the diagonal of 3D array

dim

integer defining the length of the diagonal. Default is length(x). If length(x) != 1, dim must be equal to length(x).

out

character specifying which type of diagonal to return ("vector" or "matrix"). See Details

Value

array with all elements zero except the diagonal, with dimensions c(dim,dim,dim)

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