<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Good day.<br><br>I have a question about advanced indexing.<br><br>I have 2 matrices :<br><br>>>><br>a=array([[ 0,  1,  2,  3,  4,  5],<br>            [ 6,  7,  8,  9, 10, 11],<br>         [12, 13, 14, 15, 16, 17],<br>         [18, 19, 20, 21, 22, 23]])<br><br>b=array([[1, 0, 1],<br>         [0, 2, 0],<br>         [0, 0, 3]])<br>>>><br><br>I want to put all NON-zero elements from array B into array A, but use offset!<br>This is how i did:<br><br>>>><br>mask = b!=0      # Save all non-zero values from B.<br>offset = (1,1)   # Save offset.<br>bshape = b.shape #
 Save shape of B.<br><br># Action !<br>a[ offset[0]:bshape[0]+offset[0], offset[1]:bshape[1]+offset[1] ][ mask ] = b[ mask ]<br>>>><br><br>After this, a becomes :<br><br>array([[ 0,  1,  2,  3,  4,  5],<br>       [ 6,  1,  8,  1, 10, 11],<br>       [12, 13,  2, 15, 16, 17],<br>       [18, 19, 20,  3, 22, 23]])<br><br>That's exactly what i want.<br><br>Now, my method works, but is very slow when used with big arrays. I use doule indexing, one for offset and one for mask...<br>Can anyone suggest another method, maybe with advanced indexing to do both the offset and the mask ? I am quite newbie with Numpy.<br>Or maybe just don't use the mask at all ?<br>All non-zero values from B must be inserted into A, using offset...<br><br>I tried :<br>a[ offset[0]:bshape[0]+offset[0], offset[1]:bshape[1]+offset[1], mask ]
 = b[ mask ]<br>and<br>a[ mask, offset[0]:bshape[0]+offset[0], offset[1]:bshape[1]+offset[1] ] = b[ mask ]<br>but both methods transform a into :<br>array([[ 1,  1,  1,  3,  4,  5],<br>       [ 6,  2,  8,  9, 10, 11],<br>       [12, 13,  3, 15, 16, 17],<br>       [18, 19, 20, 21, 22, 23]])<br>and that's completely wrong.<br><br>Any advice is good.<br>Thank you in advance.<br><br></td></tr></table><br>