[Numpy-discussion] Run length encoding of an ndarray

Stefan van der Walt stefan@sun.ac...
Tue Oct 2 08:36:42 CDT 2007


On Tue, Oct 02, 2007 at 01:36:02PM +0100, Michael Hoffman wrote:
> I am trying to do a type of run-length encoding of a 2D array by axis. I 
> have an array of values arranged along two axes, state and position. 
> These are many (180, 30000) uint8 arrays.
> 
> I would like to have a list of tuples like
> 
> (state, start_pos, end_pos, values)
> 
> only separating out a set of values into a new tuple if they are all the 
> same value in a run of at least 10 cells.

This snippet does run-length encoding for one row.

x = N.array([1,1,1,1,1,1,1,1,1,1,0,0,2,2,9,9,9,9,9,9,9,9,9,9])

pos, = N.where(N.diff(x) != 0)
pos = N.concatenate(([0],pos+1,[len(x)]))
rle = [(a,b,x[a]) for (a,b) in zip(pos[:-1],pos[1:])]

[(0, 10, 1), (10, 12, 0), (12, 14, 2), (14, 24, 9)]

Maybe you can use that as a starting point.

Cheers
Stéfan


More information about the Numpy-discussion mailing list