Quantcast
Channel: selective row sum matrix in numpy - Stack Overflow
Viewing all articles
Browse latest Browse all 4

selective row sum matrix in numpy

$
0
0

Is there any efficient numpy way to do the following:Assume I have some matix M of size R X C. Now assume I have another matrixE which is of shape R X a (where a is just some constant a < C), which contains row indices ofM (and -1 for padding, i.e., every element of E is in {-1, 0, .., R-1}). For example,

M=array([[1, 2, 3],         [4, 5, 6],         [7, 8, 9]])E = array([[ 0,  1],           [ 2, -1],           [-1,  0]])

Now, given those matrices, I want to generate a third matrix P, where the i'th row of P willcontain the sum of the following rows of M : E[i,:]. In the example, P will be,

P[0,:] = M[0,:] + M[1,:]P[1,:] = M[2,:]P[2,:] = M[0,:]

Yes, doing it with a loop is pretty straight forward and easy, I was wondering if there isany fancy numpy way to make it more efficient (assuming that I want to do it with large matrices,e.g., 200 X 200.

Thanks!


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>