[BioC] script line to multiply a vector by another vector or matrix

Hervé Pagès hpages at fhcrc.org
Mon Mar 16 22:27:31 CET 2009


Hi Carol,

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.

Are you trying here to describe what's commonly called "matrix multiplication"
but in the particular case where the first operand is an horizontal vector
i.e. a 1xN matrix?
Then the second operand must be an NxP matrix. When you say the 2nd operand
can be a vector or a matrix, you're just saying that P can be any value, not
just 1, is that right?
Then, according to the general rules of matrix multiplication, the final
product will be a 1xP matrix i.e. a vector of length the number of cols
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

only if you transpose a2, so that it becomes 1x2 matrix,
otherwise it's treated as a 2x1 matrix and hence is
not compatible with a1. The man page for %*% (?`%*%`)
doesn't explicitly state it but the way a vector is promoted
to a row or column matrix depends on which side of %*%
it is.

> 
> 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 will be promoted to a 1x3 matrix but
a2 is a 2x3 matrix so they are not conformable.

> a1%*%t(a2)
>      [,1] [,2]
> [1,]   14   13

yes, or use cbind() instead of rbind() in the first place.

H.

> 
> 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

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the Bioconductor mailing list