I am rewriting a MATLAB library in Julia, but I’ve noticed significant differences between Julia syntax and MATLAB. Additionally, GPT often provides incorrect responses. Is there a website that collects tables mapping MATLAB functions to their equivalent functions in Julia?
when searching the repl help for functions, you might find the function apropos
useful.
Example:
julia> apropos("least squares")
Base.:\
LinearAlgebra.qr
LinearAlgebra.pinv
LinearAlgebra.LAPACK.gels!
LinearAlgebra.qr
Base.:\
...
You can also call it by enclosing your help query with ""
, i.e.
help?> "least squares"
Base.:\
LinearAlgebra.qr
LinearAlgebra.pinv
LinearAlgebra.LAPACK.gels!
LinearAlgebra.qr
Base.:\
Bear in mind you sometimes will have to change default arguments for the same results, sometimes there isn’t a satisfactory equivalent because the implementation differs even though the results don’t, and sometimes there isn’t an equivalent at all. Less of a problem but just as annoying is that one-to-one mapping can be unidiomatic, for example MATLAB’s (possibly applies to 3+ scalars comparison max([x, y])
would be translated call-wise to Julia’s maximum([x, y])
, but max(x, y)
would be preferred to avoid the vector allocation.max([x, y, z])
instead).
You don’t have to translate/map everything (just use, or read its mapping if you want to map):
You can also just call to (or from) MATLAB:
You CAN also call to Octave, the MATLAB clone language, then you have standard functions, lose out on toolboxes. But I couldn’t use the library for that at the time, and it’s still unchanged for 4 years now.
But I could call Python (then with PyCall, now would use PythonCall), and to Octave from it… Calliing Python in-process, not to Octave from it, it still works. And I had to disable OpenBLAS threading with an env var, do get around a bug.
This might help you migrate (I did in stages), or actually not migrate, just use MATLAB code.
There IS a MATLAB code translator available online, but it doesn’t handle all syntax, nor semantic differences:
I have found that this converter frequently encounters errors. MATLAB to Julia translator | MATLAB to Julia converter
I’ve used that before, it’s pretty good for many of the obvious syntax things. It does say it’s not completely comprehensive.
This translator is not comprehensive, but it should accurately translate enough of the most common statements that most of the tedious work of translating the code by hand is eliminated.
But there won’t be one magic bullet that will translate everything in MATLAB to Julia for you (except in very simple cases), since function names, packages, and functionality are different between the two languages.
Actually, in Matlab one would write max(a, b)
like in Julia, so no translation is necessary.
Seems that way, it either changed since the last time I had to seriously use MATLAB or I mixed it up with the 3+ scalar input case max([a, b, c])
.
I have done this with a fairly large collection of solvers. The only way I could do a decent job
getting from
this Matlab and
this other Matlab
to
this Julia
was to essentially start from scratch. I rapidly found that the Matlab->Julia mapping was
less reliable, no fun, and buggy. The algorithm->Julia mapping worked a lot better and I never
had to look at my old Matlab codes at all.
I think many coders have also now experienced that direct translation from Matlab to Julia frequently results in poor performance, and un-idiomatic code.
When programming in ‘Matlab style’, Julia has few benefits to offer, since Matlab is highly optimized for vectorized function calls, with lots of temporary arrays being created and discarded. Julia, too, can vectorize calls efficiently, but is less optimized for frequent array creation/destruction.
@jackmarry If the Matlab code is quite serial and ‘loopy’, you may get a nice benefit from a fairly straightforward translation, but if it is highly optimized to Matlab’s peculiar strengths, you may be better served by doing some re-design. Quite possibly this may lead to cleaner and clearer code as well.