I need to multiply one matrix with one conditional vector, to get a vector of solutions, based on another matrix.
# Matrix A
lsA <- c(1,1,0,1,1,2,1,0,1,2,1,1,0,0,1,1,0,0,0,1)
A <- matrix(lsA,4,5, byrow = T)
# Matrix B
ls <- c("10","11","01","10","01","00","11","01","11","10","10","11","00","12","12","02","22","02","03","11")
B <- matrix(ls,4,5, byrow = T)
a1 <- c(0.128, 0.130, 0.280, 0.500, 0.650)
a2 <- c(0.055, 0.120, 0.250, 0.430, 0.600)
M1 = A%*%a1
M2 = A%*%a2
M3 <- NULL
for(i in 1:nrow(A)){
if(B[[i]]=='00' | B[[i]]=='01' | B[[i]]=='10' | B[[i]]=='11'){
M3[[i]] <- colSums(A[,i]*a1[i])
} else {
M3[[i]] <- colSums(A[,i]*a2[i])
}
The code for M3 is not working... So I want to have a vector as M1 or M2, but the elements of the matrix A should be multiplied by one or the other vector, based on the codes of B.
Any ideas?