230 likes | 256 Views
A Short Introduction to Matlab. Erik Zeitler Uppsala Database Laboratory. Matlab. MATrix LABoratory A high level programming language perform numerical computations produce graphical output Basic data type: Matrix Very fast matrix manipulation
E N D
A Short Introduction to Matlab Erik Zeitler Uppsala Database Laboratory
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. Good news: You don’t need a lot of loops. Use matrices instead. Erik Zeitler
Syntax • Matlab uses standard infix notation >> 2+5 ans = 7 >> 9-3 ans = 6 >> 12*10 ans = 120 >> 5^3 ans = 125 Erik Zeitler
Variable Assignment >> A=2+14; >> B=sqrt(A); >> C=2+3i C = 2.0000 + 3.0000i >> D=A+B+C D = 22.0000 + 3.0000i Erik Zeitler
Matrix creation Erik Zeitler
Matrix creation Erik Zeitler
Matrix Manipulation A= 1 2 3 1 2 3 Erik Zeitler
Matrix Arithmetics A= 1 2 3 1 2 3 Erik Zeitler
Some useful tricks A= 1 2 3 1 2 3 Erik Zeitler
Some useful tricks A= 1 2 3 1 2 3 Erik Zeitler
More tricks Erik Zeitler
Exercises (to do in pairs) • 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 • Given a matrix C, create a sorted vector D which contains all of the values in C which are greater than 10 Erik Zeitler
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 Erik Zeitler
if Erik Zeitler
for and while Erik Zeitler
for vs. while Interrupt execution: Ctrl-C Erik Zeitler
Functions in Matlab • Create a function fun: function [Ret1, … RetM] = fun(Arg1, …, ArgN) statements defining Ret1, …, RetM 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) >> [x1, …, xM] = fun(a1, …, aN) will set the variables x1, …, xM. Erik Zeitler
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 Erik Zeitler
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 Erik Zeitler
Exercise (to do in pairs) • 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 NewMatrix = subtract_and_scale(Matrix, Vector) Erik Zeitler
Think vectors! • Instead of [nofRows,nofCols] = size(A); for row = 1:nofRows for col = 1:nofCols A(row, col) = A(row, col) + 1 end end Write A = A + 1 More compact and much more efficient Erik Zeitler
Think vectors! • Example (script run.m): X=ones(500,500); Y=ones(500,500); tic %start timer for i=1:500 for j=1:500 Val = 0; for k = 1:500 Val = X(i,k)*Y(k,j) + Val; end Z(i,j) = Val; end end toc %stop timer and print elapsed time tic %start timer Z = X*Y; toc %stop timer and print elapsed time >> run Elapsed time is 3.47 seconds Elapsed time is 0.15 seconds Erik Zeitler
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 Erik Zeitler