python - Extracting values from indices without using loops -


so have 1 matrix containing data , vector containing information on data should extract matrix. real matrix longer below short version illustrate meant.

data matrix = array([[1 2 3],[0 3 5],[1 4 4]])  info vector = array([[1], [0], [2]]) answer matrix = array([[2 (the second element)], [0(the first element)], 4(the third element)]]) 

simple loop:

length_data = data.shapes[0] in xrange(length_data)     answer[i] = data[info[i],i] 

i know how can simple loop, how accomplish using vectorization, without usage of loops?

thanks!

slight addition question: if want answer follows

answer matrix = array([[0 2 0], [0 0 0], [0 0 4]]) 

you need zip 2 lists index first list other.

answer = map(lambda x: x[0][x[1]], zip(data, info)) # [2, 0, 4] 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -