[SciPy-user] Slicing 3D array using two 1D arrays not working
Dharhas Pothina
Dharhas.Pothina@twdb.state.tx...
Thu May 22 12:50:48 CDT 2008
I am trying to extract a subset of the original array. 'a' is a 3D array of (levels,nodes,variables). The values
So lets say I have an array where a.shape = (6,21216,2) i.e 6 levels,21216 nodes and 2 data variables and I need only the data at 1st and 4th levels and at nodes 10,100 and 2400.
I want to make a new array b where b.shape(2,3,2)
b[0,0,:] = data variables at 1st level and node 10
b[0,1,:] = data variables at 1st level and node 100
b[0,1,:] = data variables at 1st level and node 2400
b[1,0,:] = data variables at 4th level and node 10
b[1,1,:] = data variables at 4th level and node 100
b[1,1,:] = data variables at 4th level and node 2400
I'm reading through your description of ix_ to see if I understand whats happening
thanks,
- dharhas
>>> "Travis E. Oliphant" <oliphant@enthought.com> 5/22/2008 12:07 PM >>>
Dharhas Pothina wrote:
> Hi,
>
> I have an array a 3D array 'a' (a.shape = a(6, 21216, 2) ) and I want to slice it using a[levels,nodes,:] where
> levels=array([2,4]),nodes=array([12,1234,4566,1233]) etc.
>
> I can do
>
> b=a[:,nodes,:]
> c=b[levels,:,:]
>
> but if I try
> c=a[levels,nodes,:]
>
> ValueError: shape mismatch: objects cannot be broadcast to a single shape
>
> is there something I am doing wrong or is this just not possible?
>
What is a[levels, nodes, :] supposed to return? If you are expecting
the cross-product, then I suspect what you want is
il, in = numpy.ix_(levels, nodes)
c = a[il, in,:]
Indexing with a list in NumPy returns an "element-byelement" result so
the two indexing lists have to have commensurate shapes. You get the
cross-product by fiddling with the dimensions and turn levels into a
(2,1)-array and nodes into a (1,4)-array. Then broadcasting handles
createing the (2,4)-shape set of indices that you may have been expecting.
That's all numpy.ix_ does is fiddle with the shapes to give you index
arrays for getting the cross-product.
-Travis
_______________________________________________
SciPy-user mailing list
SciPy-user@scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user
More information about the SciPy-user
mailing list