Re: Dot or Inner ... but not quite
- To: mathgroup at smc.vnet.net
- Subject: [mg79648] Re: Dot or Inner ... but not quite
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Wed, 1 Aug 2007 04:58:01 -0400 (EDT)
On 7/31/07 at 6:13 AM, dot at dot.dot (Diamond, Mark) wrote:
>I have two equi-length lists, the first of number, the second of
>(equi-length) lists of numbers , such as
>l1={1,4,3}; l2={{1,7},{1,9},{5,2}};
>I want to produce 1*{1,7}+4*{1,9}+3*{5,2}
>It looks so close to Inner that I thought I could use it in some
>form, but I have ended up using
>(#[[1]]*#[[2]])& /@ Transpose[{l1,l2}]
>Is there a better way using one of the builtin functions with which
>I am unfamiliar?
Certainly, there are other ways. But better is fairly subjective.
Here are two ways to accomplish the same thing
In[7]:= Plus @@@ Thread[l1 l2]
Out[7]= {20,49}
In[8]:= Plus @@ MapThread[Times, {l1, l2}]
Out[8]= {20,49}
Note, both of these output what you said you wanted to produce, i.e.,
In[9]:= 1*{1, 7} + 4*{1, 9} + 3*{5, 2}
Out[9]= {20,49}
Which is not what your code does, i.e.,
In[10]:= (#[[1]]*#[[2]]) & /@ Transpose[{l1, l2}]
Out[10]= {{1, 7}, {4, 36}, {15, 6}}
--
To reply via email subtract one hundred and four