In Matlab there are no vectors, only single-column matrices. There are arrays with higher numbers of dimensions, however, so it treats two as a special number of dimensions. I get how they ended up there: early versions only had matrices, which gets you shockingly far, only later did they add higher dimensional arrays. However, that’s unsatisfying (why is two special?) and causes problems. Think of all the cases where Matlab does something special and different when a matrix happens to only have one column or row and something entirely different when the matrix has more than one column or row. This makes it very hard to write reliable code because it’s very common for everything to work fine until encounter data that happens to only have one column, and then – or worse still, you silently get garbage results. Example: if you do sort(A)
in Matlab, it sorts each column of A
; unless A
happens to have a single row, in which case it sorts the row. There are many similar examples. There are also no scalars, only 1x1 matrices. This can cause many problems too: since scalars are often treated specially, there’s a lot of Matlab code that breaks if you have a matrix that happens to have size 1x1.
Julia, on the other hand, distinguishes all possible dimensions of arrays, including vectors and zero-dimensional arrays. When you do size(A)
the number of dimension values you get back is the number of dimensions of the array. What you’re asking for is that size(v)
give at least two dimensions back even when v
is a vector with only one dimension. Which it could do, but again, why is two special? Why not always return three dimensions? Why not four? Maybe it should always return an infinite tuple object where all the trailing values are 1? (We could actually do that.) But instead, we return a finite tuple where the number of elements is the dimension of the array. When you’re doing m = size(v); m[2]
and it errors, you could instead do size(v, 2)
, which will give you 1 even if v
is a vector—you can ask for any dimension and if it’s more than the number of dimensions of the argument array, you’ll get 1 as the answer. Note that some people find this behavior disconcerting, but it does allow writing code that treats vectors like matrices or even higher dimensional arrays.