s-news
[Top] [All Lists]

a script line for the multiplication of a vector by another vector or ma

To: s-news@lists.biostat.wustl.edu
Subject: a script line for the multiplication of a vector by another vector or matrix
From: carol white <wht_crl@yahoo.com>
Date: Fri, 27 Feb 2009 02:27:09 -0800 (PST)
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1235730429; bh=ePcyjnjiKoYbszGhK+hIHnrmKTD6q1T6At2delsTVlc=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Reply-To:Subject:To:MIME-Version:Content-Type; b=LJY3yO4blKlehOkvuJ9m1hjWpNvVrkpWG/yhZODOX2Mf7QsyjVTdKYJmr9aNzCWwJPFnk9OUs1Dr0XXDN4s9UGTnAQNYaI1CS5a75pGn1/FoAs2ndWpLUouYzWsPgIeSJywns+61rGQLlwgFnCZ3sM0unPgRcoIp4hhsGlRjKKg=
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Reply-To:Subject:To:MIME-Version:Content-Type; b=4QkOqw5UvYZ42rcPWHDPbd6yvWlxSCKBb9dBkw7DZxJndGqg/WsrHcqMSn0YqyP5WXl2vsioH37hhhRdl4RcdXK4g63WiuuAsR0qHRqoxLOXIt1WmenOgctWEyl7zgJ0/Bfm1RJOGnr2XC7e70q4dYLqNon8ZgrEtPjWaUynpYs=;
Reply-to: wht_crl@yahoo.com
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

<Prev in Thread] Current Thread [Next in Thread>
  • a script line for the multiplication of a vector by another vector or matrix, carol white <=