I would like to multiply an array by each element of another array, and am having a lot of trouble finding a built-in or automated method of doing.

Here is an example of what I'd like to achieve:

a = 0.5;
b = 4.0;

mat = {{0, 1}, {a, b}};
Rmat = RandomInteger[10, {4, 4}]
m11 = mat[[1, 1]]*Rmat;
m12 = mat[[1, 2]]*Rmat;
m21 = mat[[2, 1]]*Rmat;
m22 = mat[[2, 2]]*Rmat;

fmat = ArrayFlatten[{{m11, m12}, {m21, m22}}]

As you can see, the first matrix mat is multiplied by each individual element of Rmat. I feel like this should be easy, but I'm not finding a built-in function that works?



3 Answers 3

ArrayFlatten[Outer[Times, mat, Rmat]]
  • $\begingroup$ Those both work thanks! $\endgroup$
    – gKirkland
    Mar 7, 2014 at 4:16
  • $\begingroup$ @rasher...good old Outer,+1 $\endgroup$
    – ubpdqn
    Mar 7, 2014 at 4:45

Try this:

ArrayFlatten[Map[Rmat*# &, mat, {2}]]

It was apparently missed by the other posters that KroneckerProduct[] is built-in:

KroneckerProduct[mat, Rmat]
  • $\begingroup$ Yep. Forgot it.... $\endgroup$
    – ciao
    Aug 21, 2015 at 19:57
  • $\begingroup$ J.M Could you help me question if you have time now? Please see here and here $\endgroup$
    – xyz
    Aug 22, 2015 at 7:40
  • $\begingroup$ @Shutao, not sure what those pictures are supposed to be. Why not write a proper question instead of a comment? $\endgroup$ Aug 22, 2015 at 8:11
  • $\begingroup$ @Guesswhoitis. I think this small question could be solved in the chatting room. $\endgroup$
    – xyz
    Aug 22, 2015 at 8:14
  • $\begingroup$ Maybe this picture can express what I need. $\endgroup$
    – xyz
    Aug 22, 2015 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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