0
|
10933 Views
|
6 Replies
|
0 Total Likes
View groups...
Share

# Multiplying lists

Posted 9 years ago
 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
Sort By:
Posted 9 years ago
 I tried it for "n=500" and it worked. I was just unsure. Many thanks !!!!!!!!!!!!!!!!!!!!!!!!!!!!You always a good job
Posted 9 years ago
 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 9 years ago
 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}
Posted 9 years ago
 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 9 years ago
 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 9 years ago
 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.