user4688329
user4688329

Reputation:

R Matrix Multiplication Error with Proper Dimensions

I am trying to multiply matrices in R. However, I am unable to do so without error. The multiplication of the dimensions seem right, but not sure what it could be. Here is some background on my data and what my loop is. Thanks for the help.

t
# [1] 6848

dim(A)
# [1] 2 2

dim(backward)
# [1] 6848    2

dim(B)
# [1] 6848    2

is.matrix(A)  
# [1] TRUE

is.matrix(backward)
# [1] TRUE

is.matrix(B)
# [1] TRUE

for (i in (t-1):1){ #FIXXXXX
  backward[i,] = t(A%*%(t(backward[i+1,])))*B[i+1,]
} 

Error in A %*% (t(backward[i + 1, ])) : non-conformable arguments

Upvotes: 1

Views: 586

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57696

By default, selecting a single row or column from a matrix results in a vector. Add drop=FALSE to your subsetting expression to keep this from happening.

t(A %*% t(backward[i+1, , drop=FALSE])) * B[i+1, , drop=FALSE]

And by the way, it would probably be a good idea to rename your t variable to something else, as t is also the transpose function.

Upvotes: 2

Related Questions