[Numpy-discussion] Slicing without a priori knowledge of dimension

Hoyt Koepke hoytak@gmail....
Wed Aug 6 08:12:35 CDT 2008

Try using slice (python builtin) to create slice objects (what is
created implicitly by :5, 1:20, etc.).  slice takes the same arguments
as range.  A list of these (7 in your case) can then be passed to
A[...] as a tuple.

That's how I would do it, but maybe someone else has a better idea or
can correct me.


On Wed, Aug 6, 2008 at 4:02 PM, Matthew Czesarski
<matthew.czesarski@gmail.com> wrote:
> Dear list.
> I've got a feeling that what I'm trying to do *should* be easy but at the
> moment I can't find a non-brute-force method.
> I'm working with quite a high-rank matrix; 7 dimensions filled with chi**2
> values. It's form is something like this:
>     chi2 = numpy.ones((3,4,5,6,7,8,9))
> What I need to do is slice out certain 2 dimensional grids that I then want
> to plot for confidence estimation; make a nice graph. This is fine: as easy
> as it gets if the 2 dimensions are known. E.g. for the 2nd and 5th axes, one
> could hardcode something like this:
>     subChi2 = chi2[ ia, ib, :, id, ie, :, ig ]
> The difficulty is that the user is to state which plane (s)he wants to slice
> out and we can't code the above. The function itself has to convert 2 rank
> numbers into an expression for a slice and I can't currently figure out how
> to do that. There are a huge number of options (well, there are 7*6=42). If
> one could just manually write a colon into a tuple like (2,2,:,2,2,:,2) or
> something even like 2,2,0:len(2ndDim),2,2,0:len(5thDim),2, things would be
> fine. But that doesn't seem to be an option. An alternative would be to set
> up 2 loops and incrementally read out the individual elements of the slice
> to a new numpy.zeros ( ( len(2ndDim), len(5thDim) ) ), but again we seem to
> be diverging from elegance.
> There must be something that I'm missing. Could somebody have a pointer as
> to what it is?
> Thanks in advance,
> Matt
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion

Hoyt Koepke
UBC Department of Computer Science

More information about the Numpy-discussion mailing list