Isaac
Isaac

Reputation: 925

Convert 2-d arrays to 1-d corresponding arrays in Python

When I make a 2 d grid with two 1-d array in python, I usually use numpy.meshgrid such as below:

x = np.arange(10)
y = np.arange(9)
xy = np.meshgrid(x,y)

However, my question is about the reverse of this process. I have 3 2-d arrays. Each array indicates lattitude and longitude and corresponding altitude. Are not there any way to convert these 2-d grids to 1-d arrays corresponding to each other in python? Sample arrays are shown like below:

x=
[[-104.09417725 -104.08866882 -104.0831604  ..., -103.8795166  -103.87399292
-103.86849976]
[-104.09458923 -104.08908081 -104.08358765 ..., -103.87991333
-103.87438965 -103.86889648]
[-104.09500122 -104.08950806 -104.08401489 ..., -103.88031006
-103.87481689 -103.86932373]
 ..., 
[-104.11058044 -104.10507202 -104.09954834 ..., -103.89535522
-103.88983154 -103.88430786]
[-104.11100769 -104.10548401 -104.09997559 ..., -103.89575195
-103.89022827 -103.88470459]
[-104.11141968 -104.10591125 -104.10038757 ..., -103.89614868 -103.890625
-103.88513184]]

y=
[[ 40.81712341  40.81744385  40.81776428 ...,  40.82929611  40.82960129
40.82990646]
[ 40.82128525  40.8216095   40.82191849 ...,  40.83345795  40.83376694
40.83407593]
[ 40.8254509   40.82577515  40.82608795 ...,  40.83763123  40.83792877
40.83824539]
..., 
[ 40.97956848  40.9798851   40.98020172 ...,  40.99177551  40.99207687
40.99238968]
[ 40.98373413  40.98405457  40.98437119 ...,  40.99593735  40.99624252
40.99655533]
[ 40.98789597  40.9882164   40.98854065 ...,  41.00011063  41.00041199
41.00072479]]

z=
[[ 1605.58544922  1615.62341309  1624.33911133 ...,  1479.11254883
1478.328125    1476.13378906]
[ 1618.58520508  1632.38305664  1645.36132812 ...,  1485.84899902
1483.43847656  1481.36865234]
[ 1640.63037109  1656.0925293   1667.14697266 ...,  1492.79797363
1488.65686035  1487.40478516]
 ..., 
[ 1599.78015137  1602.82250977  1610.40197754 ...,  1595.12097168
1594.40551758  1597.87902832]
[ 1597.80883789  1601.17883301  1607.41320801 ...,  1595.7421875
1594.26452637  1597.90893555]
[ 1596.03857422  1600.5690918   1606.30712891 ...,  1598.56982422
1594.90454102  1594.07763672]]

Any helps or ideas would be really appreciated.

Expected array would be: such as

x =
[-104.09417725 -104.08866882 -104.0831604  ..., -103.8795166  -103.87399292
-103.86849976]
y =
[ 40.82128525  40.8216095   40.82191849 ...,  40.83345795  40.83376694
40.83407593]
z = 
[ 1618.58520508  1632.38305664  1645.36132812 ...,  1485.84899902
1483.43847656  1481.36865234]

Upvotes: 0

Views: 561

Answers (2)

hpaulj
hpaulj

Reputation: 231665

meshgrid produces a 2d array for each input

In [235]: xx,yy=np.meshgrid([1,2,3],[4,5,6])

one has identical rows

In [236]: xx
Out[236]: 
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

another has identical columns

In [237]: yy
Out[237]: 
array([[4, 4, 4],
       [5, 5, 5],
       [6, 6, 6]])

Recovering the originals is just a matter of selecting a row or a column

In [238]: xx[0,:]
Out[238]: array([1, 2, 3])

In [239]: yy[:,0]
Out[239]: array([4, 5, 6])

Your x has similar, but not identical rows. So you could pick one and ignore the others. Or you could average them

In [240]: xx.mean(axis=0)
Out[240]: array([ 1.,  2.,  3.])

Or you could flatten the arrays, keeping all values

In [241]: xx.flatten()
Out[241]: array([1, 2, 3, 1, 2, 3, 1, 2, 3])

In [242]: xx.T.flatten()
Out[242]: array([1, 1, 1, 2, 2, 2, 3, 3, 3])

The similarity patten in y is less obvious. And z? meshgrid with 3 inputs would produce 3 3d arrays.

Or you could join all 3 into a 3d array

In [252]: np.dstack([xx,yy,xx+10])
Out[252]: 
array([[[ 1,  4, 11],
        [ 2,  4, 12],
        [ 3,  4, 13]],

       [[ 1,  5, 11],
        [ 2,  5, 12],
        [ 3,  5, 13]],

       [[ 1,  6, 11],
        [ 2,  6, 12],
        [ 3,  6, 13]]])

and turn that back into a 3 column array

In [253]: np.dstack([xx,yy,xx+10]).reshape(-1,3)
Out[253]: 
array([[ 1,  4, 11],
       [ 2,  4, 12],
       [ 3,  4, 13],
       [ 1,  5, 11],
       [ 2,  5, 12],
       [ 3,  5, 13],
       [ 1,  6, 11],
       [ 2,  6, 12],
       [ 3,  6, 13]])

Upvotes: 1

CT Zhu
CT Zhu

Reputation: 54380

I think this may work. In the resulting array, each row is a xyz set of the 3 initial arrays:

In [105]:
arr1 = np.random.random((2,3))
arr2 = np.random.random((2,3))
arr3 = np.random.random((2,3))

In [106]:
arr1
Out[106]:
array([[ 0.95919623,  0.76646714,  0.07782125],
       [ 0.82285529,  0.80274853,  0.28257592]])

In [107]:
arr2
Out[107]:
array([[ 0.0575891 ,  0.13063203,  0.11439967],
       [ 0.83353859,  0.72917084,  0.14294741]])

In [108]:
arr3
Out[108]:
array([[ 0.75823658,  0.09216087,  0.80364941],
       [ 0.50705487,  0.24498723,  0.3719806 ]])

In [109]:
np.dstack((arr1, arr2, arr3)).reshape((-1,3))

Out[109]:
array([[ 0.95919623,  0.0575891 ,  0.75823658],
       [ 0.76646714,  0.13063203,  0.09216087],
       [ 0.07782125,  0.11439967,  0.80364941],
       [ 0.82285529,  0.83353859,  0.50705487],
       [ 0.80274853,  0.72917084,  0.24498723],
       [ 0.28257592,  0.14294741,  0.3719806 ]])

Upvotes: 1

Related Questions