BBands wrote: > If I have a NumPy array like so: > > [[1, 12], > [2, 13], > [3, 14], > [4, 15], > [5, 16], > [6, 15], > [7, 14]] > > How can I do an inplace diff, ending up with this? > > [[1, 0], > [2, 1], > [3, 1], > [4, 1], > [5, 1], > [6, -1], > [7, -1]]
>>> import numpy as N >>> a = N.array([[1, 12], ... [2, 13], ... [3, 14], ... [4, 15], ... [5, 16], ... [6, 15], ... [7, 14]]) >>> >>> a array([[ 1, 12], [ 2, 13], [ 3, 14], [ 4, 15], [ 5, 16], [ 6, 15], [ 7, 14]]) >>> a[1:,1] = a[1:,1] - a[:-1,1] >>> a array([[ 1, 12], [ 2, 1], [ 3, 1], [ 4, 1], [ 5, 1], [ 6, -1], [ 7, -1]]) >>> a[0,1] = 0 > Also, can I covert to natural logs in place? >>> a array([[ 1., 12.], [ 2., 13.], [ 3., 14.], [ 4., 15.], [ 5., 16.], [ 6., 15.], [ 7., 14.]]) >>> N.log(a[:,1], a[:,1]) array([ 2.48490665, 2.56494936, 2.63905733, 2.7080502 , 2.77258872, 2.7080502 , 2.63905733]) >>> a array([[ 1. , 2.48490665], [ 2. , 2.56494936], [ 3. , 2.63905733], [ 4. , 2.7080502 ], [ 5. , 2.77258872], [ 6. , 2.7080502 ], [ 7. , 2.63905733]]) All ufuncs (like log() ) take an optional parameter that is the array you want the output to go to. If you pass in the same array, the operation happens in place. By the way, it looks like your first column is an index number. As The array structure keeps track of that for you, you might just as well store just the data in a single (N,) shaped array. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception [EMAIL PROTECTED] _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion