-2

I have a 2 dimensional 3x3 array e.g.:

(4,5,6

 8, 10, 12

 12,15,18 )

I would like to multiply this by a vector (1,2,3) so that I end up with a 3x3x3 array where along the third dimension all the elements of the original array are multiplied by 1, 2 or 3 respectively. How do it do this in python?

Prophet
  • 32,350
  • 22
  • 54
  • 79
218
  • 1,754
  • 7
  • 27
  • 38
  • Possible duplicate? http://stackoverflow.com/questions/3890621/matrix-and-array-multiplication-in-numpy – Savir Jun 17 '14 at 13:53
  • Do you mean a `numpy` array? A list of lists? What have you tried so far, and what happened? – jonrsharpe Jun 17 '14 at 13:53

1 Answers1

0

This is the shortest code i could come up with (not the most optimized):

a = [[1,2,3],[4,5,6],[7,8,9]]
b = [1,2,3]
mult = [[[z*x for z in y] for y in a] for x in b]
omu_negru
  • 4,642
  • 4
  • 27
  • 38