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.