I am trying to multiply two matrix in R without using %*% or crossprod. what i have tried so far
x <- matrix(1:4, ncol = 2)
y <- matrix(5:8, ncol = 2)
MatMul <- function(X,Y)
{
t(apply(x,1,crossprod,y))
}
MatMul(x,y)
I want to multiply without using crossprod or %*%
I' m totally stuck on this problem since quite some time... Hence any help is extremely welcome.
The only way I could think of solving it, is using embedded for
loops (my first ever since 2013...)
The function
MatMult <- function(x, y){
res <- matrix(NA, dim(x)[1], dim(y)[2])
for(i in seq_along(y[1, ])){
for(j in seq_along(x[, 1])){
res[j, i] <- sum(x[j, ] * y[, i])
}
}
res
}
Your matrices
x <- matrix(1:4, ncol = 2)
y <- matrix(5:8, ncol = 2)
Testing
MatMult(x, y)
## [,1] [,2]
## [1,] 23 31
## [2,] 34 46
x%*%y
## [,1] [,2]
## [1,] 23 31
## [2,] 34 46