Unintuitive Julia result: selecting a row from a matrix

Yup, there absolutely is a reason and it’s a doozy. It turns out that the embedding of one-dimensional containers into the algebra of matrices is not obvious. There are a number of choices you must make in how they behave, impacting everything from indexing to transposes. It’s one of the few design decisions that has had so much thought put into it that many Julia folk who have been around for a while have it memorized (#4774). It’s spawned a paper (still a draft) and presentations. There’s even an :4774: emoji :4774: commemorating its author and major thought-leader here and on Slack.

The end-result, though, is a remarkably simple rule: The resulting dimensionality of an indexing expression is the sum of the dimensionalities of the indices. Or even more specifically, the resulting axes of an indexing expression is the concatenation of all the axes of all the indices used.

So that’s why X[1, :] is a 1-dimensional vector while X[[1], :] is a 1xN row matrix.

30 Likes