2

I try to understand what happens with this code using ndarrays:

max_evecs = evecs[..., :, 0]

where evecs is of type ndarray. So far I know that ':' is a slicing operator, and '...' is a so called Ellipse. So far ':' means all elements and '...' refers to as many as needed. I investigated the arrays with shape:

>>> max_evecs.shape
(128, 128, 72)
>>> evecs.shape
(128, 128, 72, 3)

I could imagine that, it is tried to convert a 3D array into an array of triples (x,y,z). But I am not sure about that.

For those who care: its from the dipy software package: function quantize_evecs inside https://github.com/nipy/dipy/blob/ff75b192f694cdb62cc11310159cdb652ce62073/dipy/reconst/dti.py line around 1663

math
  • 8,514
  • 10
  • 53
  • 61
  • If you simply want to get a handle on the entire array (e.g. in order to assign to value in a for loop), you can use either `A[:]` or `A[...]` – Yibo Yang Mar 19 '17 at 17:16

1 Answers1

4

From http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

Ellipsis expand to the number of : objects needed to make a selection tuple of the same length as x.ndim. Only the first ellipsis is expanded, any others are interpreted as :.

So in your example, evecs has ndim equal to 4, and the following are equivalent:

evecs[..., :, 0]
evecs[:, :, :, 0]

It is also helpful to try things out interactively to get a feel for it. With some simple data, for example a = np.random.rand(3, 3, 2), try printing out a and various slices of it like a[..., 0] and a[1, ...] and similar and see how they are related.

YXD
  • 31,741
  • 15
  • 75
  • 115
  • I looked into the doc, there they have a `x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])` and then run: `x[...,0]` which should result in `array([[1, 2, 3],[4, 5, 6]])` However I get `array(0)` for this example. This does not make sense to me. – math Dec 12 '13 at 10:56
  • For this example, `x.ndim` is 1. So `x[..., 0]` is the same as `x[0]` (there are no dimensions/axes remaining after specifying the 0). – YXD Dec 12 '13 at 11:06
  • 1
    An alternate explanation from [here](http://stackoverflow.com/a/118508/553404). The ellipsis means "at this point, insert as many full slices (:) to extend the multi-dimensional slice to all dimensions." – YXD Dec 12 '13 at 11:11
  • Thanks, to all of you. The python doc, just uses another x array than I thought: `x = np.array([[[1],[2],[3]], [[4],[5],[6]]])` – math Dec 12 '13 at 12:13