Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

For example I have this matrix:

mk = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

and I'd like it to multiple with this:

ot = Table[5, {4}]
{5,5,5,5} 

to get this one:

{{{ 5, 5, 5, 5}, {10,10,10,10}, {15,15,15}}, 
 {{20,20,20,20}, {25,25 ...}...}...}

but KroneckerProduct gives me 3 rows instead of a 3 x 3 x 4 matrix (similar but not identical). Of course I have really large lists...

KroneckerProduct[mk, ot]
{{5, 5, 5, 5, 10, 10, 10, 10, 15, 15, 15, 15}, 
 {20, 20, 20, 20, 25, 25, 25, 25, 30, 30, 30, 30}, 
 {35, 35, 35, 35, 40, 40, 40, 40, 45, 45, 45, 45}}
share|improve this question
    
Use Map at the second level Map[{5, 5, 5, 5} # &, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {2}]. –  Artes Dec 16 '13 at 12:10
2  
How about Outer[Times, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {5, 5, 5, 5}]? –  Jacob Akkerboom Dec 16 '13 at 12:11
    
Thank you! These were very helpful! –  szms Dec 16 '13 at 12:17

1 Answer 1

You almost had it:

m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
v = {5, 5, 5, 5};

KroneckerProduct[m, {{v}}]
{{{5, 5, 5, 5}, {10, 10, 10, 10}, {15, 15, 15, 15}}, {{20, 20, 20, 20}, {25, 25, 25, 
   25}, {30, 30, 30, 30}}, {{35, 35, 35, 35}, {40, 40, 40, 40}, {45, 45, 45, 45}}}

At least in version 7 Outer as recommended by Jacob is somewhat faster however:

m = RandomReal[9, {50, 100}];
v = RandomReal[9, 127];

KroneckerProduct[m, {{v}}] ~Do~ {500} // Timing // First
Outer[Times, m, v] ~Do~ {500}         // Timing // First
1.373

0.733
share|improve this answer
    
+1 and the timings are similar in proportion on version 9 –  Jacob Akkerboom Dec 16 '13 at 12:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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