crossprod(x, y = NULL) tcrossprod(x, y = NULL)
| x | matrix or vector, numeric or complex. Missing values (NA) are allowed. |
| y |
matrix or vector, numeric or complex. Must have the same number of
rows as x. If not specified or NULL (the default),
x is used. Also, for tcrossprod, y must have
the same number of columns as x.
Missing values (NA) are allowed. |
| crossprod | returns a matrix representing the cross product of x and y, defined as t(x) %*% y, where %*% is matrix multiplication and t is transposition. Thus the [i,j]th element of the result is sum(x[,i]*y[,j]). |
| tcrossprod | returns a matrix representing the cross product of t(x) and t(y), defined as x %*% t(y), where %*% is matrix multiplication and t is transposition. Thus the [i,j]th element of the result is sum(x[i,]*y[j,]). |
amat <- matrix(c(19,8,11,2,18,17,15,19,10), nrow = 3)
crossprod(amat) # if amat has dimensions of n(rows) and p(cols)
# the resultant matrix will be p by p
bmat <- c(9, 5, 14)
crossprod(amat, bmat)
tcrossprod(1:3, 11:13) # same as outer(1:3, 11:13)