Matlab debootstrapping #1
• 586 words • 3 min • updated
⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.
Alternative title: “Matlab, second impressions”. I am trying to learn Matlab super fast for prototyping purposes. Fast learning isn’t my preferred style for approaching a new programming language. However, this is necessary at the moment.
Disclaimer: This is not a tutorial. Just annotations and notes. Information is not necessarily correct, so consume it at your own risk.
Matlab Paradise #
- Everything is optimized for matrices and vectors. Matlab means “matrix laboratory”.
- A number is treated as a 1x1 matrix. This is really interesting and has nice implications and syntactic sugar.
- Matrix manipulation:
A = [1,2,3]is a row vector;B = [1;2;3]is a column vector;C = [1,2;3,4]mixes them together. - Slicing: Python style, but with different syntax.
1:10meansrange(1,10)and1:2:5meansrange(1,5,2)but Matlab is inclusive (5 is present). - Indexing vectors:
A(1,2),A(2,1). Beware: indexes begin from 1, not zero. - Performance: slow, slow, slow. See benchmarks on Julia’s homepage.
matlab -nodesktopfor command-line prompt. Self-contained, and you can still browse help withdoc luorhelp lu.- Introspection #1:
whos. Know everything about your variables. - Workspace view: Really nice. Save variables with
save out.mat. Load them withload out.mat. - Some Unix goodness is available:
ls,cd,pwd,ls -l. - However,
mkdirandrmdirwork differently. - Hello, world!:
fprintf('string here')ordisplay('hello, world!'). - Strings are enclosed with single quotes, not double. Why?
- Functions: C/C++/Java style.
f(a,b,c,d,e). - Some functions have more than one return value:
ret1, ret2 = f(a,b,c). - Clear screen:
C-lorclc. - Beware!
cleardeletes your variables.clear varis more specific.clear allis even more destructive. - Auto-completion with TAB. No
C-Space. - Need help?
lu(then F1. Hints?C-F1. - Matlab hotkeys seem optimized for emacs users. See
C-y(yank/paste),C-k(kill/copy),C-aandC-e(navigation),C-s(search),C-_(undo). open file.mto open files without the fancy GUI.delete file.m. Notrm.- Comments with percent sign (%). Why not hashes?
- Run a Matlab script:
cdinto its directory then typefile<CR>in the prompt. version! R2014a == 8.3.0.xC-cfor long computations.rand(2,2)if you ran out of creativity for numbers.v = [1 2]equalsv = [1,2]. Who needs commas?- Element-wise operators are preceded with a dot. Example:
.*. - Love intelligent prompts:
C-p,M-p,C-n,M-n,<UP>,<DOWN>. - Miss your shell?
system('echo Hello world!'). - File handling:
fid = fopen('file.txt','w'); fprintf(fid,'...'); fclose(fid);
Introspection #
which lu.m,which luedit lu.m,edit luhelp <command>spits outputdoc <command>opens help browserwhosshows your variables and symbols and functionstype <command>. Like Unix’scat.- Try double-clicking on a variable in your workspace
path
Internals: Functions, variables, types, control flow #
sum(a), min(a), max(a), median(a), var(a), prod(a), mean(a), size(a), mode(a), std(a)falseis 0.trueis 1. Case sensitive!isvector(a), isempty(a)- Not is a tilde
~, not!(C-style) - Matrix indexing: Column 2:
a(:,2); Line 2:a(2,:). inv(a)for inversefind (a < 3)filter2^3 == 8- Quick matrix generation:
zeros, ones, eye, magic, rand - Math:
cos(pi), sin(pi), tan(pi), tanh(pi) - More math:
rad2deg(pi) == 180, deg2rad det(m), rank(m), trace(m), a,b = lu(m)if, elseif, else, end. No parenthesis required.for k=1:10; display('hey'); endwhileswitch b; case 1; display('one'); end. No colon after cases.
Symbolic math library/toolbox #
syms x y zshorthand forx = sym('x')x = sym('x', [2 1])for a symbolic column vectorf = x + y^2symbolic functionsubs(f, [x y], [1 2])evaluates the functiongradient(f), hessian(f), ccode(f)magic functions!
Fancy stuff #
diary on,diary off,help diaryto record terminal sessions