7
$\begingroup$

I have two list which are not equal dimension:

listA = {{{1, 2}, {2, 2}, {3, 4}}, {{1, 1}, {2, 2}, {3, 3}}};
listB = {{4, 4}, {5, 5}};

And I want to divide them or multiply the such as the results has the same dimensions, for the first dimension, it should look like:

(#*listB[[1]]) & /@ listA[[1]]

but I would like to do it all at once so that the result has the same dimension as listA. I bet it has probably been answered here but I cannot seem to find it. Therefore link to an existing answer is also great.

$\endgroup$
  • 1
    $\begingroup$ How about Table[(#*listB[[i]]) & /@ listA[[i]], {i, Length[listB]}] $\endgroup$ – bill s Sep 23 '16 at 21:18
  • 2
    $\begingroup$ MapThread[(Function[{x}, x #2] /@ #1) &, {listA, listB}]? $\endgroup$ – march Sep 23 '16 at 21:48
  • $\begingroup$ Related: (23395) $\endgroup$ – Mr.Wizard Sep 24 '16 at 6:30
5
$\begingroup$

Some overhead but the most clean solution I could muster:

Diagonal@Outer[Times, listB, listA, 1, 2]

{{{4, 8}, {8, 8}, {12, 16}}, {{5, 5}, {10, 10}, {15, 15}}}

$\endgroup$
2
$\begingroup$

Using Simon Woods's smartThread one may write:

smartThread[listA*listB, 1]
{{{4, 8}, {8, 8}, {12, 16}}, {{5, 5}, {10, 10}, {15, 15}}}

For fun here is a way using Inactive and Activate, assuming list elements are atomic:

Map[Inactive, Quiet[listA*listB], {-2}] // Activate
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.