[Numpy-discussion] slicing an n-dimensional array

2014-12-03 Thread Moroney, Catherine M (398E)
Hello,

I'm sure there's a simple solution, but I'm not seeing it so any hints would be
greatly appreciated.

I have an array A of shape (NX, NY, NZ), and then I have a second array B 
of shape (NX, NY)
that ranges from 0 to NZ in value.

I want to create a third array C of shape (NX, NY) that holds the B-th 
slice for each (NX, NY).

For example:

A = numpy.zeros((NX,NY,NZ))
A[:,:,0] = numpy.arange(0, NX*NY).reshape(NX,NY)
A[:,:,1] = numpy.arange(1, NX*NY+1).reshape(NX,NY)

and so on

B = numpy.zeros((NX,NY))
B[0,0] = 0
B[0,1] = 1

so C[0,0] = A[0,0,B[0,0]] 
   C[0,1] = A[0,1,B[0,1]] 
   ...
   C[NX-1,NY-1] = A[NX-1,NY-1,B[NX-1,NY-1]]

   and C has shape(NX,NY)

How do I accomplish this without loops?

Thank-you for any advice,

Catherine
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] median filtering a masked array

2014-11-05 Thread Moroney, Catherine M (398E)
Hello,

I have to perform a median filter on only selected elements in an array and am
wondering if using a masked array will speed things up by preventing the 
computation
of the median on those elements where it's not needed.

I'm using a Fortran code to fill up a (NX,NY,NZ) array where (NZ) is the number 
of
points in the median filter window and NX and NY are the array dimensions 
themselves.

If a filter is not required at a certain (I,J) element, I set all the elements 
along
the NZ dimension to a fill value, and then create a masked array where the 
mask
argument is set to True for all fill values.  

So, I am hoping that the numpy.median(data, axis=2) call will not even try to 
compute
the median for those (I,J) elements where all the data in the NZ direction are 
masked
out, thus saving me valuable computational time.

Is this a valid assumption?  Or does numpy go ahead and calculate the median 
for all
(I,J) elements irregardless of whether they're masked out or not?

What is the recommended way of doing a fast median filter on an array where only
certain elements of the array need to be calculated?  I'm trying to avoid a 
nested loop over all (I,J) elements.

Catherine 
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion