well its not really slow. yet with np.where it seems to be 2x faster for large arrays :
a[1:4,1:4] = np.where(mask,b,a[1:4,1:4]) otherwise consider Cython: http://docs.cython.org/docs/numpy_tutorial.html#tuning-indexing-further Robert Cristi Constantin wrote: > > Thank you so much for your prompt answer, Stéfan. > It's a very very interesting method. I will keep it for future. :) > > But, i tested it with a few examples and the speed of execution is just > a tiny bit slower than what i told you i was using. So it's not faster, > it's about the same speed. > > Thank you again. I will play with your method a little more. > > --- On *Thu, 6/18/09, Stéfan van der Walt /<[email protected]>/* wrote: > > From: Stéfan van der Walt <[email protected]> > Subject: Re: [Numpy-discussion] Advanced indexing advice? > To: "Discussion of Numerical Python" <[email protected]> > Date: Thursday, June 18, 2009, 2:16 AM > > Hi Cristi > > 2009/6/18 Cristi Constantin <[email protected] > </mc/[email protected]>>: > > I have a question about advanced indexing. > > > > I have 2 matrices : > > > >>>> > > a=array([[ 0, 1, 2, 3, 4, 5], > > [ 6, 7, 8, 9, 10, 11], > > [12, 13, 14, 15, 16, 17], > > [18, 19, 20, 21, 22, 23]]) > > > > b=array([[1, 0, 1], > > [0, 2, 0], > > [0, 0, 3]]) > >>>> > > > > I want to put all NON-zero elements from array B into array A, > but use > > offset! > > Here's a solution using views: > > offset = np.array([1,1]) > slices = [slice(*x) for x in zip(offset, offset + b.shape)] > c = a[slices] > mask = (b != 0) > c[mask] = b[mask] > > Regards > Stéfan > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Numpy-discussion mailing list > [email protected] > http://mail.scipy.org/mailman/listinfo/numpy-discussion _______________________________________________ Numpy-discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
