[SciPy-user] Create n-dim array from axis

Tom K. tpk@kraussfamily....
Tue Mar 24 20:45:01 CDT 2009




josef.pktd wrote:
> 
> On Tue, Mar 24, 2009 at 11:35 AM, Vincent Schut <schut@sarvision.nl>
> wrote:
> 
>>> a made-up example
>>> x = [2, 3, 5, 10]
>>> y = [0, 1]
>>> z = [100, 200, 500, 1000]
>>> create_array(x, y, z)
>>> ?
>>>
>> Hmm, tricky...
>> I'd go for something like this:
>>
>> def create_array2(nodes):
>>     # nodes is a list with lists of axis points (e.g. [[2, 3, 5, 10],
>> [0, 1], [100, 200, 500, 1000]]
>>     shape = tuple([len(a) for a in nodes])
>>     idx = numpy.indices(shape)
>>     result = numpy.zeros(idx.shape, numpy.float)
>>     for d in range(idx.shape[0]):
>>         result[d] = numpy.take(nodes[d], idx[d])
>>     return result.transpose()
>>
> 

Neat problem.

Here's an implementation that avoids creating the index arrays by using
broadcasting:

def create_array(*vectors):
    views = [vec.view() for vec in vectors]
    out = zeros([len(vectors)] + [vec.size for vec in vectors])
    for viewIdx, view in enumerate(views):
        shape = [1]*len(views)
        shape[viewIdx] = -1
        view.shape = shape
        out[viewIdx,:] = view
    return out

Examples below.  Note, you can still transpose if you want but it feels more
natural to me to have the 1st dimension be the "axis" so g[i] is the i-th
grid.

In [126]: x = array([2, 3, 5, 10]) 

In [127]: y = array([0, 1] )

In [128]: z = array([100, 200, 500, 1000]) 

In [129]: g2=create_array(x, y)

In [130]: g2.shape
Out[130]: (2, 4, 2)

In [131]: g2[0]
Out[131]: 
array([[  2.,   2.],
       [  3.,   3.],
       [  5.,   5.],
       [ 10.,  10.]])

In [132]: g2[1]
Out[132]: 
array([[ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.]])

In [133]: g3=create_array(x, y, z)

In [134]: g3.shape
Out[134]: (3, 4, 2, 4)

In [135]: g3[0]
Out[135]: 
array([[[  2.,   2.,   2.,   2.],
        [  2.,   2.,   2.,   2.]],

       [[  3.,   3.,   3.,   3.],
        [  3.,   3.,   3.,   3.]],

       [[  5.,   5.,   5.,   5.],
        [  5.,   5.,   5.,   5.]],

       [[ 10.,  10.,  10.,  10.],
        [ 10.,  10.,  10.,  10.]]])

In [136]: g3[1]
Out[136]: 
array([[[ 0.,  0.,  0.,  0.],
        [ 1.,  1.,  1.,  1.]],

       [[ 0.,  0.,  0.,  0.],
        [ 1.,  1.,  1.,  1.]],

       [[ 0.,  0.,  0.,  0.],
        [ 1.,  1.,  1.,  1.]],

       [[ 0.,  0.,  0.,  0.],
        [ 1.,  1.,  1.,  1.]]])

In [137]: g3[2]
Out[137]: 
array([[[  100.,   200.,   500.,  1000.],
        [  100.,   200.,   500.,  1000.]],

       [[  100.,   200.,   500.,  1000.],
        [  100.,   200.,   500.,  1000.]],

       [[  100.,   200.,   500.,  1000.],
        [  100.,   200.,   500.,  1000.]],

       [[  100.,   200.,   500.,  1000.],
        [  100.,   200.,   500.,  1000.]]])


-- 
View this message in context: http://www.nabble.com/Create-n-dim-array-from-axis-tp22655632p22693686.html
Sent from the Scipy-User mailing list archive at Nabble.com.



More information about the SciPy-user mailing list