tcrossprod_ij computes cross product between two sets of rows of a matrix.

tcrossprod_ij(X, id1, id2)

Arguments

X

matrix

id1

vector of integers specifying the list of rows of X (first set)

id2

vector of integers specifying the list of rows of X, (second set), same length as id1.

Value

matrix containing the cross product of X[id1, ] and X[id2, ].

Examples


set.seed(42)
X <- matrix(rnorm(5 * 3), 5, 3)

id1 <- c(1, 3)
id2 <- c(5, 4)

(s1 <- matric::tcrossprod_ij(X, id1, id2))
#>           [,1]      [,2]
#> [1,] 0.3869236 0.2896411
#> [2,] 0.3379025 0.4259471

(s2 <- tcrossprod(X)[id1, id2])
#>           [,1]      [,2]
#> [1,] 0.3869236 0.2896411
#> [2,] 0.3379025 0.4259471

all.equal(s1, s2)
#> [1] TRUE