[SciPy-user] Create n-dim array from axis
Vincent Schut
schut@sarvision...
Tue Mar 24 10:35:11 CDT 2009
>
> 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.
More information about the SciPy-user
mailing list