Why does iterating over a multidimensional array return the individual elements?

Surely if you wanted to get the individual elements you would not be using a multidimensional array in the first place?

Why? What would you expect it to do?

Iterate over the first axis. If someone is using an ndim array, it seems to me they would do so because the values contained have an underlying structure (as in columns might represent different samples with the values in each column being a feature), which the user will want to use accordingly. Why otherwise even bother defining a ndim array?

Why would the first axis necessarily be uniquely privileged?

I have iterated over multi-dimensional arrays a zillion times, and wanted the individual elements 97% of the time. If the first axis is special, you can use a vector of vectors, or an array of vectors.

Yeah, that’s what I think of doing. Or just make a bunch of views. I never would’ve expected it to iterate column-by-column without me explicitly asking for that.

I think this is different from other languages because in something like MATLAB you will sometimes use a matrix for an “array of higher dimensional numbers”. Like an array of quaternions is a matrix where each column is the 4 pieces of the quaternion. In Julia… you just use an array of StaticArrays, or an array of Quaternions because it’s faster anyways and then will have a bunch of nice dispatches. In that case, the reason to put things into a matrix is because you want the matrix structure for linear algebra operators which will just work via dispatches.

That’s not to say that iterating column-by-column is never needed, but it’s far from being the obvious answer. Anyways, you can just make a columns(A) iterator and then

for c in columns(A)

end

will give you what you want.

I have iterated over multi-dimensional arrays a zillion times, and wanted the individual elements 97% of the time.

Why are you defining multi-dimensional arrays if you are iterating over them sequentially?

If the first axis is special, you can use a vector of vectors, or an array of vectors.

I’ve been talking about ndimensional arrays. According to the docs I’ve read a vector in julia is equivalent to a 1dim array, and a matrix is equivalent to a 2dim array. So how exactly is using them going to help me when they are identical to the ndim arrays about which I am complaining about?

Because they may be referring to, for example, values collected at spatial x, y and z positions (or any other set of parameters). But there’s nothing inherently special about the x-axis. And so very often I want to find all values greater than 2, or take the absolute value of everything or whatever.

If the first axis is truly special, like the time axis of a set of signals, using an array of vectors would emphasize that.

I’m sorry, I don’t understand this question.

I’m not sure you’ll find a satisfactory answer here. It’s just a choice that we’ve found to work well, and it’s self-consistent and generally powerful.

It means that the number of elements in an array is equal to the product of its size is equal to the number of iterations you get.

It means that the element type of an array is the same as the type iteration returns is the same as the type indexing returns.

3 Likes