[Numpy-discussion] avoiding loops when downsampling arrays
eat
e.antero.tammi@gmail....
Tue Feb 7 08:27:24 CST 2012
Hi
This is elegant and very fast as well!
On Tue, Feb 7, 2012 at 2:57 PM, Sturla Molden <sturla@molden.no> wrote:
> On 06.02.2012 22:27, Sturla Molden wrote:
> >
> >
> >>
> >> # Make a 4D view of this data, such that b[i,j]
> >> # is a 2D block with shape (4,4) (e.g. b[0,0] is
> >> # the same as a[:4, :4]).
> >> b = as_strided(a, shape=(a.shape[0]/4, a.shape[1]/4, 4, 4),
> >> strides=(4*a.strides[0], 4*a.strides[1], a.strides[0],
> a.strides[1]))
> >>
> >
> > Yes :-) Being used to Fortran (and also MATLAB) this is the kind of
> mapping It never occurs for me to think about. What else but NumPy is
> flexible enough to do this? :-)
>
> Actually, using as_strided is not needed. We can just reshape like this:
>
> (m,n) ---> (m//4, 4, n//4, 4)
>
> and then use np.any along the two length-4 dimensions.
>
> m,n = data.shape
> cond = lamda x : (x <= t1) & (x >= t2)
>
I guess you meant here cond= lambda x: (x>= t1)& (x<= t2)
> x = cond(data).reshape((m//4, 4, n//4, 4))
> found = np.any(np.any(x, axis=1), axis=2)
>
Regards,
eat
>
>
> Sturla
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120207/7a9cfc8f/attachment.html
More information about the NumPy-Discussion
mailing list