Skip to contents

Define function to change the diagonal of array

Usage

diag4Darray(x) <- value

Arguments

x

3D array whose diagonal must be c hanged

value

vector defining the new values of diagonal.

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.

Examples

x <- array(1, dim = c(4,4,4,4))
diag4Darray(x) <- c(2,2,2,2)
x
#> , , 1, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    2    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 2, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 3, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 4, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 1, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 2, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    2    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 3, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 4, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 1, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 2, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 3, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    2    1
#> [4,]    1    1    1    1
#> 
#> , , 4, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 1, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 2, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 3, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    1
#> 
#> , , 4, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    1    1    1
#> [3,]    1    1    1    1
#> [4,]    1    1    1    2
#> 
# , , 1, 1
#
#      [,1] [,2] [,3] [,4]
# [1,]    2    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 2, 1
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 3, 1
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 4, 1
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 1, 2
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 2, 2
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    2    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 3, 2
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 4, 2
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 1, 3
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 2, 3
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 3, 3
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    2    1
# [4,]    1    1    1    1
#
# , , 4, 3
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 1, 4
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 2, 4
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 3, 4
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    1
#
# , , 4, 4
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    1    1    1    1
# [3,]    1    1    1    1
# [4,]    1    1    1    2