Michael Jackson; M to J; Matlab to Julia
-matlab has % comments and %{ }% block comments -julias has # comments
-matlab: ones(3) -julia: ones(3,3) -other examples: zeros, randn, rand
-matlab: sqrt(-1) == 1i -julia: sqrt(-1) throws DomainError
-magic -keyboard
MATLAB | Julia | |
numel | length | |
mod | % | |
bitxor | $ | |
bitor | PIPE | |
bitand | & | |
fprintf | @sprintf | |
length | x -> maximum(size(x)) | |
disp | println | display doesn’t add a new line |
from: func(…) to: ans = func(…)
from: x = 3 to: x = 3 println(“x = “, x) from: 3 to: ans = 3 println(“ans = “, ans) **
-matlab strings can have either ” or ’ -julia strings have ” and chars have ’
-matlab: size(3) == [1 1] -julia: size(3) == ()
-matlab: 3i or 4j -julia: 3im
-julia: must first perform using PyPlot
-matlab: a = @(b,c) b + c -julia: a(b,c) = b + c OR a = (b,c) -> b + c
-matlab functions have optional `end`s -matlab function define return valuesat the start -matlab functions can be called without parameters: func
from: function [a b] = f() … end to: function f() … [a b] end
-matlab: M(a,b) -julia: M[a,b]
-in matlab, [1,2,3;4,5,6] is valid -in julia the commas would have to be spaces
-in matlab, [1 2] > 0 works -in julia, this is a type error
-matlab can continue lines with ‘…’
b = 1 a = b, c = [2 3]
in matlab: a = b = 1; c = [2 3] in julia: a = [2 3]; b = 2; c = 3
-matlab has both ~= and != -julia has only !=
-julia can’t compare scalars with matrices with normal operators (have to use dot version)
A = randn(3,4) -matlab: A(:, [3 1 2]) % reorders -julia: A[:, [3 1 2]] % errors
-matlab: A(1, :) = [] % deletes row -julia: A[1, :] = [] # errors
-fopen, load, save, input
-doc function_handle -help something.m
-setting A = B copies B in matlab
-use A = copy(B) for this
matlab uses parentheses to index and call functions
matlab can have dynamic behavior depending on the number of arguments
solution -set nargin and nargout to constants, add TODO whenever either variable is used
-output formatting format long -plot formatting hold on
in matlab, ~3 == 0, and integers can be used in if statements
it seems the rules for matrix transposition are not easily parsable with the current algorithm (string starts if not after matlab literal)
this gets more complicated with things like case statements which can’t be detected with regex
-add no space around braces indexing
-transform matlan_call to call function (to not be confusing when in an array) matlab_call(… same for indexing
-transform anonymous functions
-max(blah, [], 2) -> maximum(blah, 2)
-functions and matrices
-0 argument function calling
-cast if / elseif tests to bools
-built in function names
-replace 1:3 with [1:3]
converting something like: func(3,4,[1 2])[3,4](2, 1) to func(3,4,[1 2])[3,4][2, 1]
-keep history -detect what hasnt been declared yet, etc -gather information on matrices/functions -if uses end as argument: matrix -if assigned to: matrix -if given as a function: function
sample translator noteworthy differences between julia and matlab julia performance tips julia manual julia stdlib julia base stdlib fast numeric computation in julia matlab symbol reference