[Tutor] 1d to 2d array creation

Bala subramanian bala.biophysics at gmail.com
Mon Oct 1 23:04:42 CEST 2012


Friends,
I have an 1d array like a=[1, 1, 2, 2, 2, 3, 3, 1, 1, 1], i have to
convert it to 2d array for plotting as follows. The 2d array is filled
by a[colum index] to obtain the new array shown below.

[ [ 1.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  1.,  1.],
  [ 0.,  0.,  2.,  2.,  2.,  0.,  0.,  0.,  0.,  0.],
  [ 0.,  0.,  0.,  0.,  0.,  3.,  3.,  0.,  0.,  0.] ]

I wrote the following simple code for the conversion. However i guess
there should be more fancy/speeder way to do that. Also i need to
create such 2d-array from larger 1d arrays of size 20000,30000 items
etc. Hence i would like to request hints for a better code for the
purpose.

Here no. rows in my case is always = no. of discrete values in array a.

>>>my=1
>>>for i in range(3):
>>>      for j in range(10):
>>>             if a[j] == my : b[i,j]=my
>>>             else: b[i,j]=0
>>>      my +=1

Thanks,
Bala


More information about the Tutor mailing list