Reputation: 249
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]]]
Upvotes: 2
Views: 10894
Reputation: 500447
Here is a way to do it using pure Python:
a3 = [[[el * 3 for el in col] for col in row] for row in a]
This works with 3D matrices of any shape, not just 1x1x3.
However, if this is the sort of thing you need to do on a regular basis, I would encourage you to learn NumPy. Then you'll be able to write:
a3 = a * 3
Upvotes: 1
Reputation: 26586
Here's one way using list comprehensions:
>>> a = [[[1, 2, 3]]]
>>> b = [[x*3 for x in a[0][0]]]
>>> b
[[3, 6, 9]]
Upvotes: 1
Reputation: 29111
Try:
a= [[[1, 2, 3]]]
for i in range(len(a[0][0])):
a[0][0][i] *= 3
print a
Upvotes: 1