On Tue, May 3, 2011 at 18:58, Michael Katz <[email protected]> wrote: > So I was trying to find a "pure numpy" solution for this. I then learned > about fancy indexing and boolean indexing, and saw that I could do boolean > array version: > > mapped_colors = np.zeros(unmapped_colors.shape, dtype=np.uint32) + gray > mapped_colors[unmapped_colors==BNA_LAND_FEATURE_CODE] = green > mapped_colors[unmapped_colors==BNA_WATER_FEATURE_CODE] = blue > > and in fact I could do "fancy indexing" version: > > color_array = np.array((green, blue, gray), dtype=np.uint32) > colors = color_array[unmapped_colors] > > The boolean array version is pretty simple, but makes three "passes" over > the data. > > The fancy indexing version is simple and one pass, but it relies on the fact > that my constant values happen to be nice to use as array indexes. And in > fact to use it in actual code I'd need to do one or more other passes to > check unmapped_colors for any indexes < 0 or > 2.
Also, still not *quite* as general as you might like, but sufficient for the problem as stated: colors = color_array[np.clip(unmapped_colors, 0, 2)] -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
