python_tensorflow
python_tensorflow

Reputation: 349

R: non-conformable arguments

I'm trying to multiply a 2x2 matrix with 1x2 matrix.

class(logP)
# "array"
dim(logP)
# 2 2 1
class(theta[,t-1,1])
# "numeric"
dim(theta[,t-1,1])
# NULL
length(theta[,t-1,1])
# 2
logP%*%theta[,t-1,1]
# Error in logP %*% theta[, t - 1, 1] : non-conformable arguments     

logP is 2x2 while theta[,t-1,1] is 2x1. How should I perform matrix multiplication with them?

Upvotes: 0

Views: 8683

Answers (1)

lmo
lmo

Reputation: 38500

The main problem is that logP is a 2X2X1 array. You should convert it to a matrix using drop.

Here is a reproducible example that replicates your problem.

# the identity matrix stored as a 2X2X1 array
logP <- array(c(1, 0, 0, 1), c(2,2,1))
# some vector
theta <- 1:2

Inspecting the objects as in the post

dim(logP)
[1] 2 2 1
dim(theta)
NULL

Make sure we properly set up the identity matrix

logP
, , 1

     [,1] [,2]
[1,]    1    0
[2,]    0    1

Now, try the multiplication

logP %*% theta

Error in logP %*% theta : non-conformable arguments

Since the third dimension is 1, we can get rid of it using drop.

drop(logP) %*% theta
     [,1]
[1,]    1
[2,]    2

which returns what we would expect a 2X1 matrix with the values of theta.

Upvotes: 2

Related Questions