Reputation: 5965
I'm trying to calculate across the row of a matrix and store that value in a different matrix. Is this the most efficient way to do this or are there any built in functions I should be aware of.
import numpy as np
a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ])
def calc_across(matrix):
frame = []
for row in matrix:
frame.append( [row[0] * row[1]/2. * row[2]/3] ) # period present to generate floats
return np.array(frame)
b = calc_across(a)
If I do print b
I get the following matrix:
b = [ [1.], [20.], [84.] ]
If a
is 3x3, b
must be 3x1 (3 rows, 1 column). If a
is 10x3, b
must be 10x1, etc.
Upvotes: 0
Views: 915
Reputation: 13274
Try:
b = np.prod(a / [1.0,2.0,3.0],axis=1, keepdims=True)
b
# # array([[ 1.],
# [ 20.],
# [ 84.]])
I hope this helps.
Upvotes: 2