script line to multiply a vector by another vector or matrix
1
0
Entering edit mode
carol white ▴ 680
@carol-white-2174
Last seen 8.9 years ago
European Union
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 [[alternative HTML version deleted]]
• 628 views
ADD COMMENT
0
Entering edit mode
Patrick Aboyoun ★ 1.6k
@patrick-aboyoun-6734
Last seen 9.7 years ago
United States
Carol, I think if you write down the rules to how you would like your operation to behave, you will find the solution and if not, we could help you out. I thought I could decipher what the rules were but the operation 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 got the Sesame Street song "One of these things is not like the others" stuck in my head. Patrick carol white wrote: > 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 > > > > > [[alternative HTML version deleted]] > > > -------------------------------------------------------------------- ---- > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
ADD COMMENT

Login before adding your answer.

Traffic: 722 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6