# Matrix product giving error "Error in base::crossprod(x, y) : non-conformable arguments" even when order of matrices are coorect

I made 2 matrices given as

#matrix 1
X<-matrix(rnorm(100),nrow=47,ncol=1,byrow =TRUE)
X<-cbind(X,rnorm(100))
X<-cbind(X,1)

# matrix 2 transpose of matrix 1
X_transpose<-t(X)



When I take their product

A<-X*Xt
#Error pops up
Error in X * Xt : non-conformable arrays

#OR

A<-crossprod(X,Xt)
#error pops up
Error in base::crossprod(x, y) : non-conformable arguments

#Even dimensions of matrices are:-
dim(Xt)
  3 47
dim(X)
 47  3 Why matrices are not multiplying even when their order satisfies the matrices product condition
(m X n) * (n X p) resulting matrix will have order (m X p)

Thanking you for the help..

If A and B are matrices, A*B tries to perform elementwise multiplication, which is possible only if they are of same dimensions, which is not the case here.

To multiply two matrices, use %*% operator. You can also use crossprod or tcrossprod, but then you don't need to take transpose previously. Here's what the documentation of crossprod says:

Given matrices x and y as arguments, return a matrix cross-product. This is formally equivalent to (but usually slightly faster than) the call t(x) %% y (crossprod) or x %% t(y) (tcrossprod).

Also, you can't fit 100 elements in a 47 \times 1 matrix, and since you have only 1 column, byrow argument is not required.

# setting seed
set.seed(seed = 23660)

# matrix 1
X <- matrix(data = rnorm(n = 47),
nrow = 47)

X <- cbind(X,
rnorm(n = 47),
1)

# matrix 2: transpose of matrix 1
X_t <- t(x = X)

# matrix multiplication
a <- X %*% X_t
b <- crossprod(x = X_t)
c <- tcrossprod(x = X)

# checking their equality
all.equal(a, b, c)
#>  TRUE


Created on 2019-02-12 by the reprex package (v0.2.1)

Hope this helps.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.