extract-methods: Extract or Replace Subtensors

Description Usage Arguments Details Value Examples

Description

Extends '[' and '[<-' from the base array class for the Tensor class. Works exactly as it would for the base 'array' class.

Usage

1
2
3
4
5
## S4 method for signature 'Tensor'
x[i, j, ..., drop = TRUE]

## S4 replacement method for signature 'Tensor'
x[i, j, ...] <- value

Arguments

x

Tensor to be subset

i, j, ...

indices that specify the extents of the sub-tensor

drop

whether or not to reduce the number of modes to exclude those that have '1' as the mode

value

either vector, matrix, or array that will replace the subtensor

Details

x[i,j,...,drop=TRUE]

Value

an object of class Tensor

Examples

1
2
3
4
5
6
7
8
9
tnsr <- rand_tensor()
tnsr[1,2,3]
tnsr[3,1,]
tnsr[,,5]
tnsr[,,5,drop=FALSE]

tnsr[1,2,3] <- 3; tnsr[1,2,3]
tnsr[3,1,] <- rep(0,5); tnsr[3,1,]
tnsr[,2,] <- matrix(0,nrow=3,ncol=5); tnsr[,2,]

Example output

Numeric Tensor of 1 Modes
Modes:  1 
Data: 
[1] -0.6770678
Numeric Tensor of 1 Modes
Modes:  5 
Data: 
[1] -0.5950218732  0.0008453985  0.4318944629  0.6538046395  0.5847901444
Numeric Tensor of 2 Modes
Modes:  3 4 
Data: 
           [,1]       [,2]        [,3]       [,4]
[1,]  1.3680175  0.8712612 -0.16440414  1.9616244
[2,] -1.9625257 -0.4597705 -0.14336628 -0.8536279
[3,]  0.5847901 -0.9430118  0.07099026 -1.2856887
Numeric Tensor of 3 Modes
Modes:  3 4 1 
Data: 
[1]  1.3680175 -1.9625257  0.5847901  0.8712612 -0.4597705 -0.9430118
Numeric Tensor of 1 Modes
Modes:  1 
Data: 
[1] 3
Numeric Tensor of 1 Modes
Modes:  5 
Data: 
[1] 0 0 0 0 0
Numeric Tensor of 2 Modes
Modes:  3 5 
Data: 
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    0    0    0    0
[3,]    0    0    0    0    0

rTensor documentation built on May 15, 2021, 9:06 a.m.