[Numpy-discussion] Replacing values based on index
Geordie McBain
gdmcbain@freeshell....
Thu Aug 12 00:34:08 CDT 2010
2010/8/12 Pinner, Luke <Luke.Pinner@environment.gov.au>:
> Apologies if this has been asked many times before, but I'm having
> trouble even coming up with the right terms to search on.
>
> I have an x,y,date stack of satellite images from which I need to know
> on what date the max and min values occur. I can calculate the index at
> which the min/max values occur, but how do I join that back to the date?
>
> A simple example case:
>
> import numpy as np
>
> #Date when image was captured,
> #images are in the stack in this order
> days=[10, 20, 31, 41, 51, 59, 69, 79, 90, 100]
>
> #dummy array representing something like
> #"stackofimages.argmin(axis=2)"
> indexmin=np.array([[0, 2, 1],
> [6, 7, 3],
> [5, 5, 8]])
>
> #I want to convert indexmin to "daymin" i.e
> #[[10, 31, 20],
> # [69, 79, 41],
> # [59, 59, 90]]
> #Where the each element of indexmin represent the value
> #at the corresponding index in the days list/array.
>
> #What I am currently doing is below,
> #which is _rather_ slow on large arrays
> #as I'm calling np.where len(days) times
> daymin=np.zeros(indexmin.shape).astype(np.int)
> for i,d in enumerate(days):
> daymin[np.where(indexmin==i)]=d
>
> Any better suggestions?
Convert days to np.array, then you can index it with your indexmin:
days = [10, 20, 31, 41, 51, 59, 69, 79, 90, 100]
mydays = np.array (days)
indexmin=np.array([[0, 2, 1],
[6, 7, 3],
[5, 5, 8]])
print mydays[indexmin]
=>
[[10 31 20]
[69 79 41]
[59 59 90]]
More information about the NumPy-Discussion
mailing list