Say, I have a numpy array like this:
[[ 1 2 3 5]
[ 4 5 6 8]
[ 7 8 9 11]]
I want to get the sum of (col 0+col 2) and (col1+col3) for each row. I know its probably elementary, but cant get it. Kindly help
Well yes it's pretty elementary once you know the assignment scheme of numpy arrays :
Let
x = [[ 1 2 3 5]
[ 4 5 6 8]
[ 7 8 9 11]]
y1 = x[:,0]+x[:,2] # Sum of columns 0 and 2
y1 = array([4,10,16])
y2 = x[:,1]+x[:,3] # Sum of columns 1 and 3
y2 = array([7,13,19])
This should do the trick:
import numpy as np
mat=np.arange(12).reshape(3,4)
A=np.sum(mat,axis=0)[0::2]
B=np.sum(mat,axis=0)[1::2]
A = (col 0+col 2)
B = (col1 + col3)