A Short Introduction to Matlab Erik Zeitler Uppsala
A Short Introduction to Matlab Erik Zeitler Uppsala Database Laboratory Erik Zeitler
Matlab § MATrix LABoratory • A high level programming language § perform numerical computations § produce graphical output § Basic data type: Matrix • Very fast matrix manipulation § Programming control structures are available • procedures • loops § , but these are slow. 2021 -12 -31 Good news: You don’t need a lot of loops. Use matrices instead. Erik Zeitler 2
Syntax § Matlab uses standard infix notation >> 2+5 ans = 7 >> 9 -3 ans = 6 >> 12*10 ans = 120 >> 5^3 ans = 125 2021 -12 -31 Erik Zeitler 3
Variable Assignment >> A=2+14; >> B=sqrt(A); >> C=2+3 i C = 2. 0000 + 3. 0000 i >> D=A+B+C D = 22. 0000 + 3. 0000 i 2021 -12 -31 Erik Zeitler 4
Matrix creation >> A=1: 5 A = 1 2 3 >> A=1: 2: 7 A = 1 3 5 7 4 2021 -12 -31 >> A=[1 2 3 4 5] A = 5 1 2 3 4 5 >> A=[1 2 3; 1 2 3] A = 1 2 3 Erik Zeitler 5
Matrix creation >> zeros(3, 5) ans = 0 0 0 0 >> ones(3, 4) ans = 1 1 1 2021 -12 -31 >> B=[1 2 3] B = 1 2 3 >> A=[B; B] A = 1 2 3 Erik Zeitler 6
Matrix Manipulation >> A' ans = 1 1 2 2 3 3 >> A==3 ans = 0 0 1 A= 123 >> A(3, : ) = [2 3 4] A = 1 2 3 2 3 4 >> rot 90(A) >> A(: , 2) >> A(: , 4) = [4; 4] ans = A = 3 3 2 1 2 3 4 2 2 2 1 2 3 4 1 1 2021 -12 -31 Erik Zeitler 7
Matrix Arithmetics >> A+A ans = 2 4 6 6 >> A. *A ans = 1 4 9 2021 -12 -31 A= 123 >> A*A ? ? ? Error using ==> mtimes Inner matrix dimensions must agree. >> A*A' ans = 14 14 >> A'*A ans = 2 4 4 8 6 12 Erik Zeitler 6 12 18 8
Some useful tricks A= 123 >> repmat(A, 1, 2) ans = 1 2 3 >> find(A==3) ans = 5 6 >> randperm(4) ans = 2 4 3 1 >> size(A) ans = 2 3 2021 -12 -31 Erik Zeitler 9
Some useful tricks >> sum(A) ans = 2 4 6 >> sum(A, 2) ans = 6 6 >> sum(A)) ans = 12 2021 -12 -31 A= 123 >> sort(randperm(4)) ans = 1 2 3 4 Erik Zeitler 10
More tricks >> B=[1 2 3; 4 5 6] B = 1 2 3 4 5 6 >> std(B) ans = 2. 1213 >> mean(B) ans = 2. 5000 3. 5000 >> var(B) ans = 4. 5000 2021 -12 -31 4. 5000 Erik Zeitler 11
Exercises (to do in pairs) 1. Given a (1*N) vector A, create a (3*N) matrix B such that • • • the first row in B contains A the second row in B contains A backwards the third row in B contains the sum of A and A backwards 2. Given a matrix C, create a sorted vector D which contains all of the values in C which are greater than 10 2021 -12 -31 Erik Zeitler 12
Other useful functions § princomp(Data) • Returns the principal components. § plot(A, B, Symbol) • where Symbol is one of ['+' 'o' '*' etc…] • hold on/off • grid on/off 2021 -12 -31 Erik Zeitler 13
if if condition statements else statements end 2021 -12 -31 >> A=[0 1 2]; >> B=[1 2 3]; >> if A>B C=A-B elseif B>A C=B-A else C=A+B end Erik Zeitler 14
for and while for i = vector statements end while condition statements end % example: j=0; for i=1: 10 j=i+j end % example: j=0, i=1 while i<=10 j=i+j, i=i+1 end 2021 -12 -31 Erik Zeitler 15
for vs. while j=1: 2; a=0; Interrupt execution: Ctrl-C for i=1: size(j) if j(i)==1 j=[j, i] else a=a+j(i) end 2021 -12 -31 while(i<=size(j)) if j(i)==1 j=[j, i] else a=a+j(i) end Erik Zeitler 16
Functions in Matlab § Create a function fun: function [Ret 1, … Ret. M] = fun(Arg 1, …, Arg. N) statements defining Ret 1, …, Ret. M end § This definition must go in a file called fun. m § Invoking fun from Matlab (make sure your cd to the dir where fun. m is) >> [x 1, …, x. M] = fun(a 1, …, a. N) will set the variables x 1, …, x. M. 2021 -12 -31 Erik Zeitler 17
traparea. m function Area = traparea(A, B, H) % Calculates the area of a trapezoid % with length A, B of the parallell % sides and the distance H between the % sides. Area=0. 5*(A+B)*H end 2021 -12 -31 Erik Zeitler 18
cylinder. m function [Vol, Area] = cylinder(R, H) %calculates the volume and surface %area of a cylinder with radius R and %height H Area=2*pi*R*H; Vol=(Area*R)/2; end 2021 -12 -31 Erik Zeitler 19
Exercise (to do in pairs) 1. Write a function subtract_and_scale: Given this input: § § a matrix of dimension N*M, a vector of size 1*M Calculate a new matrix, by § § subtracting the vector from each row in the matrix, multiplying each value in the new matrix by 2 function New. Matrix = subtract_and_scale(Matrix, Vector) 2021 -12 -31 Erik Zeitler 20
Think vectors! § Instead of [nof. Rows, nof. Cols] = size(A); for row = 1: nof. Rows for col = 1: nof. Cols A(row, col) = A(row, col) + 1 end Write A = A + 1 More compact and much more efficient 2021 -12 -31 Erik Zeitler 21
Think vectors! § Example (script run. m): X=ones(500, 500); Y=ones(500, 500); >> run tic %start timer Elapsed time is 3. 47 seconds for i=1: 500 for j=1: 500 Elapsed time is 0. 15 seconds Val = 0; for k = 1: 500 Val = X(i, k)*Y(k, j) + Val; end Z(i, j) = Val; end toc %stop timer and print elapsed time tic %start timer Z = X*Y; toc %stop timer and print elapsed time 2021 -12 -31 Erik Zeitler 22
Learn more § Pärt-Enander, Sjöberg, Pihl: Användning av Matlab • English version is available too • For sale at UTH-Gård § There is an abundance of Matlab tutorials on the internet 2021 -12 -31 Erik Zeitler 23
- Slides: 23