Julia is a lot more consistent, generic and general than Matlab and this is a pretty good example of that. Letās look at what Matlab has for maxāthe core āmethodsā ignoring weird flags are these:
M = max(A)
M = max(A,[],"all")
M = max(A,[],dim)
M = max(A,[],vecdim)
C = max(A,B)
Even just the first āmethodā is actually a pretty wild amalgam of behaviors. To quote:
- If
A is a vector, then max(A) returns the maximum of A.
- If
A is a matrix, then max(A) is a row vector containing the maximum value of each column of A.
- If
A is a multidimensional array, then max(A) operates along the first dimension of A whose size is greater than 1, treating the elements as vectors. The size of M in this dimension becomes 1, while the sizes of all other dimensions remain the same as in A. If A is an empty array whose first dimension has zero length, then M is an empty array with the same size as A.
- If
A is a table or timetable, then max(A) returns a one-row table containing the maximum of each variable.
Uhh, what? Is there, like, a function that just gives me the maximum value of an array, regardless of how many dimensions it has? Ah, ok, there it is, itās the next methodāthe delightfully named max(A,[],"all") method. Iām really seeing everyoneās point hereāwriting max(A,[],"all") really is much nicer than maximum(A). Iām kidding, of course, thatās awful, but that is how maximum is spelled in Matlab.
Ok, what if we come at it from the other end and compare Juliaās max with Matlabās? Matlab only documents supporting pairwise max(A, B) not n-ary max(A, B, Cā¦) like Julia. Also, it assumes that A and B are some kind of array and you want to do an elementwise max operation. Thatās fine for Matlabās world view, but what if you have objects that arenāt arrays? Shocking concept, I know, but in that case, you might want to compare n values using something besides elementwise scalar comparison. For example, itās often useful to compare arrays or tuples as objects themselves using lexicographical ordering. In fact, this is what Julia does when you apply max to arrays:
julia> max([1,2,3], [2,3], [])
2-element Vector{Int64}:
2
3
This can be confusing to Matlab users, but itās a very useful general programming behavior and is similar to how strings work:
julia> max("apple", "zoo", "cat")
"zoo"
Of course Matlab only got strings in 2016, so that was hardly a consideration for them, but Julia has had them from the start. In Julia, if you want to do elementwise max you use broadcasting:
julia> max.([1,2,3],[3,2,1])
3-element Vector{Int64}:
3
2
3
Give the existence of generic n-ary max in Julia, people wanting max([1, 3, 2]) to return 3 should consider that a general n-ary max should actually just return its argument when called with a single argument, i.e. max([1, 3, 2]) should return its argument, which is [1, 3, 2]. We donāt actually define a 1-ary max because it would be too much of a potential footgun for unsuspecting Matlab refugees, but thatās what the method should do if it existed.
Anyway, I too find it slightly annoying when I occasionally forget and try to do max(v) and it doesnāt work, but you should keep in mind that Matlabās max function is a byzantine mess and that the since max and maximum are fundamentally different concepts, any attempt to fuse them in a language that wants to be consistent, general and generic is bound to end badly.