Skip to content

Instantly share code, notes, and snippets.

@cuckookernel
Last active March 18, 2018 20:20
Show Gist options
  • Save cuckookernel/9784889 to your computer and use it in GitHub Desktop.
Save cuckookernel/9784889 to your computer and use it in GitHub Desktop.
MATLAB to Julia quick translation/conversion reference guide

A quick and dirty MATLAB to Julia translation/conversion reference guide.

This is not meant as a reference to the language. For that you should read the manual

Important Diferences

The first few are drawn from here

  • Use brackets to index into vectors and matrices, i.e. do v[i] instead of v(i).
  • Array assigment is done by reference, i.e after A = B modifying A will modify B!
  • One dimensional vectors are column vectors by default.
  • [17, 42, 25] and [17;42;25] both create a column vector. To create a row vector do [17 42 25], this is really a 1-by-3 (two-dimensional matrix).
  • The imaginary unit sqrt(-1) is represented by im.
  • You don't need semicolons at the end of lines to supress output. You can still have them if you like them.
  • Files don't have to be named after the main function of the file. A file can contain many modules, a module can contain many functions and has to explicitely export some for them to be available externally.
  • Trying to access an undefined variable or an entry of a vector which is out of bounce will produce an error (throw an exception)

Important similiarities

The syntax of Julia seems to be heavily inspired in MATLAB's. So, a there are more similiraties than differences. Here is a short list of some of the things that are (roughly) the same:

  • Getting help on a function help( sin )
  • Ranges 10:-3:2 (fast range not fully expanded in memory) and [10:-3:2] (fully expanded in memory)
  • Control flow structures -- note no parenthesis around if and while expressions:
    • if <boolean-expr> ... elseif else ...end
    • for i = 1:n ... end.
    • while <boolean-expr> ... end.
  • function declarations (function myfun(arg1, arg2) ... end ). Note, however that there are no named output arguments preciding function name. Functions can return several values by returning a tuple, like so: return (out1,out2,out3).
MATLAB Julia Comments
fun = @(x,y) x + y fun = (x,y) -> x + y
cell(3) cell(3,3) This produces an Array{Any,2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment