[Numpy-discussion] Manipulate neighboring points in 2D array

Zachary Pincus zachary.pincus@yale....
Thu Dec 27 17:28:50 CST 2012


> I have 2D array, let's say: `np.random.random((100,100))` and I want to do simple manipulation on each point neighbors, like divide their values by 3.
> 
> So for each array value, x, and it neighbors n:
> 
> n n n    n/3 n/3 n/3
> n x n -> n/3  x  n/3
> n n n    n/3 n/3 n/3
> 
> I searched a bit, and found about scipy ndimage filters, but if I'm not wrong, there is no such function. Of course me being wrong is quite possible, as I did not comprehend whole ndimage module, but I tried generic filter for example and browser other functions.
> 
> Is there better way to make above manipulation, instead using for loop over every array element?

I am not sure I understand the above manipulation... typically neighborhood operators take an array element and the its neighborhood and then give a single output that becomes the value of the new array at that point. That is, a 3x3 neighborhood filter would act as a function F(R^{3x3}) -> R. It appears that what you're talking about above is a function F(R^{3x3}) -> R^{3x3}. But how is this output to map onto the original array positions? Is the function to be applied to non-overlapping neighborhoods? Is it to be applied to all neighborhoods and then summed at each position to give the output array?

If you can describe the problem in a bit more detail, with perhaps some sample input and output for what you desire (and/or with some pseudocode describing how it would work in a looping-over-each-element approach), I'm sure folks can figure out how best to do this in numpy.

Zach


More information about the NumPy-Discussion mailing list