Problem: extracting a row from an array, returns a column

Yeah, I agree with DNF that N[1:1, :] is probably more idiomatic.

One way to understand it is that a 1D Vector is not the same as an Nx1 Matrix (i.e. a column), even though they are often interchangeable.

The indexing rules in Julia are agnostic to the dimensionality of the array, i.e. they work the same with 1D, 2D, 3D, etc.

See this post with some more examples of indexing and dimensionality. The general rule is that when you index dimension D, the dimesionality of the result for that dimension is the same as the dimension of the index, so if you index with a zero-dimensional index (a scalar) that dimension is dropped. If you index with a 1-dimensional index (a vector, range, etc.) the dimension is retained. If you index with a 2D index you actually add a dimension.

I think (though I haven’t verified exhaustively) that you can write the general rule as

resultsize(idxs...) = tuple(Iterators.flatten(size.(idxs))...)

i.e. the size of the result is the concatenation of the sizes of the indices.

So 1D and 2D Arrays aren’t treated specially when it comes to indexing.

3 Likes