pythonmatrixmatrix-multiplicationscalar

matrix multiplication with a constant


How can I multiply the following 1x1x3 matrix with a constant value (scalar):

a = [[[1, 2, 3]]]

For example, multiplying a by 3 should produce the following:

a*3 = [[[3,6,9]]]

Solution

  • Use NumPy:

    In [1]: import numpy as np
    
    In [2]: a = np.array([[[1, 2, 3]]])
    
    In [3]: a
    Out[3]: array([[[1, 2, 3]]])
    
    In [4]: a*3
    Out[4]: array([[[3, 6, 9]]])