Hi, I want to multiply a vector (1st argument) by another vector or a matrix (2nd argument) depends if a column or a row of a matrix or a subset of a matrix is taken. The first argument presents a coefficient vector (of 1 or more elements) that will be multiplied by all elements of the 2nd argument. Therefore, the final product (multiplication of the 2 arguments) will be a vector of the length of the number of rows of the 2nd argument.
I wanted to use %*% for all cases, either taking directly the 2nd argument or its tansposal but it doesn't work for all cases. Do I have to test the type of 2nd argument and write a different script lines or can I use a script line for all cases?
for example, I will have the following different cases:
a1 = c(1) a2 = c(2) res = a1*a2 = 2 c(1)%*%c(2)
[,1] [1,] 2
a1 = c(1) a2 = c(1,2) res = a1*a2 = [3] sum(c(1)*c(1,2)) to get 3 here, %*% is not of help
a1 = c(1,2) a2 = c(2,1) res = 4 c(1,2)%*%c(2,1) so a1%*%a2 would work
a1 = c(1,2) a2 = rbind(c(1,2), c(2,1)) res = [5 4] a1%*%a2 [,1] [,2] [1,] 5 4
a1 = c(1,2,3) a2 = rbind(c(1,2,3), c(2,1,3)) a1%*%t(a2) [,1] [,2] [1,] 14 13
etc
|
|