crossprod
Matrix Cross Product

Description

Returns a matrix that is the cross product of two given matrices.

Usage

crossprod(x, y = NULL)
tcrossprod(x, y = NULL)

Arguments

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.

Details

It is better to use crossprod(x) than x %*% t(x) because crossprod(x) is faster, and, on some machines, is more accurate.
Any computation involving NA results in NA.
Vectors are taken to be columns, so crossprod(vec1,vec2) is a one-by-one matrix, with the element being the dot product of the two vectors.
tcrossprod(vec1,vec2) is the outer product of the two vectors.
Value
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,]).
See Also
matrix, Matrix-product, outer.
Examples
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)

Package base version 6.1.1-7
Package Index