Message Boards Message Boards

Multiplying lists

Given the two lists; how do you multiply them? L1 = {{X1 , y1}, {X2 , y2}, {X3 , y3}, {X4, y4},………...,{Xn, yn}} L2 = {{X1 , z1}, {X2 , z2}, {X3 , z3}, {X4, z4},…………,,{Xn, zn}} To obtain: L1L2 = {{ X1 , y1 z1}, { X2 , y2* z2}, { X3 , y3* z3}, { X4 , y4* z4},……………},{Xn, yn*zn}

Attachments:
6 Replies

I tried it for "n=500" and it worked. I was just unsure. Many thanks !!!!!!!!!!!!!!!!!!!!!!!!!!!!

You always a good job

sorry. missed the answer:

In[57]:= Transpose[{Transpose[L1][[1]], 
  Transpose[L1][[2]] Transpose[L2][[2]]}]

Out[57]= {{X1, y1 z1}, {X2, y2 z2}, {X3, y3 z3}, {X4, y4 z4}}
POSTED BY: Hans Dolhaine

Can this result be extended for "n" elements as:

Given the two lists; how do you multiply them? L1 = {{X1 , y1}, {X2 , y2}, {X3 , y3}, {X4, y4},………...,{Xn, yn}} L2 = {{X1 , z1}, {X2 , z2}, {X3 , z3}, {X4, z4},…………,},{Xn, zn}} To obtain: L1L2 = {{ X1 , y1 z1}, { X2 , y2* z2}, { X3 , y3* z3}, { X4 , y4* z4},……………},{Xn, yn*zn}

Yes. Just try it.

But note, the code given by Sean Clarke,

Transpose[{L1[[All, 1]], L1[[All, 2]]*L2[[All, 2]]}]

which is basically the same, looks a bit mor elegant.

Regards HD

POSTED BY: Hans Dolhaine

I

n[52]:= L1 = {{X1, y1}, {X2, y2}, {X3, y3}, {X4, y4}}
 L2 = {{X1, z1}, {X2, z2}, {X3, z3}, {X4, z4}} 

Out[52]= {{X1, y1}, {X2, y2}, {X3, y3}, {X4, y4}}

Out[53]= {{X1, z1}, {X2, z2}, {X3, z3}, {X4, z4}}

will do it. regards

POSTED BY: Hans Dolhaine

It looks like X1, X2 etc are indexes or keys of some kind... is that right? You're using a list like a dictionary-like data structure. If that's the case you probably want to use Associations instead of lists. Associations are the correct datastructure for this situation. You can solve this issue with Merge:

Merge[{<|a -> 1, b -> 2|>, <|a -> 4, b -> 5|>}, Apply[Times]]

Here "a" and "b" are the keys. We merge them and Apply Times.

For lists, it's not clear how you want to handle edge cases, but you can do the following:

Transpose[{L1[[All, 1]], L1[[All, 2]]*L2[[All, 2]]}]

{{X1, y1 z1}, {X2, y2 z2}, {X3, y3 z3}, {X4, y4 z4}}

I'll let you put together how that works.

POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract