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

josef.pktd@gmai... josef.pktd@gmai...
Tue Mar 24 11:15:26 CDT 2009


On Tue, Mar 24, 2009 at 11:35 AM, Vincent Schut <schut@sarvision.nl> wrote:
>>
>> This is a good example for using slices and mgrid and is much better
>> for regularly spaced grids (representable of slices) than what I had.
>> However, what I also needed in the past was building a grid for
>> arbitrary points in each dimension
>>
>> 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()
>
> create_array2([[0.0, 0.5, 1], [2.0, 2.5, 3]])
>
>
> though this stil contains a loop which might be avoidable... It ought to
> be pretty generic though. It's the end of the working day here, and I'm
> starting to get a bit fuzzy in the brains, so better solutions might
> very well be possible.
>
> Vincent.
>

Thanks,
it's still very fast for a few dimensions (3-5) and works very well, I
needed a few million entries in the constructed matrix before seeing a
delay in interactive use.

Josef


More information about the SciPy-user mailing list