MATLAB examples - introduction A = [ 16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1 ] A A' sum(A) (matlab tends to operate on columns -> returns row vector) sum(A') sum(A')' (returns a column vector of row sums) A(4,2) (row first, then column, just like in normal matrix notation) A(2,4) A(4,5) (should give error message) X = A X(4,5) = 17 (assignment always works, though, even if have to expand) 1:10 (returns row vector of integer range) 100:-7:50 0:pi/4:pi A(1:4,4) (select 4th column) A(:,4) (shorthand for selecting all of available range) A(:,end) (refers to last column) A(:,[1 3 2 4]) (swap rows - think of this as matlab making a reasonable interpretation of a vector being used in place of a scalar) (more accurately, matlab thinks "naturally" in terms of matrices. A scalar is simply a 1x1 matrix) A(8) (8th element - equivalent to A(4,2)) variables: created automatically as needed - no typing (like C has). this means you have to be a bit more careful about typos. also consider that names are unique up to a user-defined length: namelengthmax clear (delete name - CHECK) numbers: 3 -99 0.01 1.6e-20 6.022e23 1i -3.14j (also complex - i or j the same) 3e5i internal format is 64-bit IEEE format: ~16 digits, exponent 10^+-308 operators: + - * / (as you'd expect) \ (left division) ^ (power) ' (complex conjugate transpose) ( ) functions: help elfun help specfun help elmat (many functions as you'd expect) pi i j Inf NaN rho = (1+sqrt(5))/2 a = abs(3+4i) huge = exp(log(realmax)) toobig = pi * huge matrix generation: zeros ones rand (uniformly distributed random numbers) randn (normally distributed) load (m-files - scripts) matrix manipulation: B = [ A A+32; A+48 A+16]; (concatenation by []'s) X = A; X(:,2) = [] (deletes 2nd column) X X(1,2) = [] (error - trying to delete a single element, but other elements in row/column are left) X(2:2:10) = [] (single dimension - so considered as a row vector. now deletion is valid) (but you could change values of elements individually. also a range) X(1:2,2:3) = 0 det(A) eig(A) other operators: .* (element-by-element multiplication) ./ .\ .^ .' (unconjugated array transpose) n = (0:9)'; pows = [n n.^2 2.^n] x = (1:0.1:2)'; logs = [x log10(x)] logical subscripting (different from reordering rows - apparently need a matrix of 0 and 1 exactly the same size as the matrix): x = [ 2.1 1.7 1.6 1.5 NaN 1.9 1.8 1.5 5.1 1.8 1.4 28 1 ]; x = x(isfinite(x)) (gets rid of NaN; isfinite() gives 0 only in column position of NaN) x = x(abs(x-mean(x)) <= 3*std(x)) (removes outliers, > 3 sigma from mean) A(~isprime(A)) = 0 (zeros non-prime elements of matrix) suppressing output: end with a semicolon (especially useful in loops where you don't need the intermediate values) entering long statements: ... basic plots: x = -1:.1:1; y = x.^3; plottools (click on both x and y using shift-click in variable workspace. then right-click for menu, select plot(x,y)) (get m-code from File menu) x = 0:pi/100:2*pi; y = sin(x); plot(x,y) y2 = sin(x-0.25); y3 = sin(x-0.5); plot(x,y,x,y2,x,y3) [x,y,z] = peaks (example of 3-valued function) pcolor(x,y,z) shading interp hold on (continue plotting in same space) contour(x,y,z,20,'k') hold off reset figure properties: clf reset subplots: t = 0:pi/10:2*pi; [X,Y,Z] = cylinder(4*cos(t)); subplot(2,2,1); mesh(X) subplot(2,2,2); mesh(Y) subplot(2,2,3); mesh(Z) subplot(2,2,4); mesh(X,Y,Z) 3D plots [X,Y] = meshgrid(-8:.0.5:8); R = sqrt(X.^2 + Y.^2) + eps; Z = sin(R)./R; (eps prevents divide by 0) mesh(X,Y,Z,'EdgeColor','black') surf(X,Y,Z) colormap hsv colorbar surf(X,Y,Z) colormap hsv alpha(.4) surf(X,Y,Z,'FaceColor','red','EdgeColor','none') camlight left; lighting phong View -> Camera Toolbar select Rotate3D tool other examples: look up "animate" in the online help chaos example