[SciPy-User] Rerranging an array

Lutz Maibaum lutz.maibaum@gmail....
Tue Nov 9 15:37:15 CST 2010

On Nov 9, 2010, at 12:27 PM, Jose Gomez-Dans wrote:
> I've been struggling to find a quick and clean way to do this. I have a 2d array where each gridcell is either 0 or some integer. I then have another array with the following setup
> [ id_number, observation]. The id_number refer to the first array, and indicates the position where observation goes. I basically want to convert the second array into the same shape of the first array, but apart from using lots of loops (the arrays are fairly large), all my other attempts are failing miserably.

Is the id_numer a single (flat) index of the position in the file? If your array of observations is called a, and you want to create a two-dimensional matrix of shape (n,m) you could try (not tested):

b=zeros(n*m, dtype=int)
b[a[:,0]] = a[:,1]
b = b.reshape((n,m))

Hope this helps,


More information about the SciPy-User mailing list