The numpy way to do this is to use np.choose or fancy indexing / accepting (see below):
m = array([[1, 2], [4, 5], [7, 8], [6, 2]]) select = array([0,1,0,0]) result = np.choose(select, mT)
This way there is no need for python loops or anything else, with all the speed advantages numpy gives you. mT simply necessary because the choice is really more than the choice between the two arrays np.choose(select, (m[:,0], m[:1])) , but its direct way to use it like this.
Using fantasy indexing:
result = m[np.arange(len(select)), select]
And if speed is very important, np.take , which works on a 1D representation (for some reason, it's a little faster, but maybe not for these tiny arrays):
result = m.take(select+np.arange(0, len(select) * m.shape[1], m.shape[1]))
source share