1 / 23

A Short Introduction to Matlab

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

ptaggart
Download Presentation

A Short Introduction to Matlab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Short Introduction to Matlab Erik Zeitler Uppsala Database Laboratory

  2. 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

  3. Syntax • Matlab uses standard infix notation >> 2+5 ans = 7 >> 9-3 ans = 6 >> 12*10 ans = 120 >> 5^3 ans = 125 Erik Zeitler

  4. 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

  5. Matrix creation Erik Zeitler

  6. Matrix creation Erik Zeitler

  7. Matrix Manipulation A= 1 2 3 1 2 3 Erik Zeitler

  8. Matrix Arithmetics A= 1 2 3 1 2 3 Erik Zeitler

  9. Some useful tricks A= 1 2 3 1 2 3 Erik Zeitler

  10. Some useful tricks A= 1 2 3 1 2 3 Erik Zeitler

  11. More tricks Erik Zeitler

  12. 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

  13. 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

  14. if Erik Zeitler

  15. for and while Erik Zeitler

  16. for vs. while Interrupt execution: Ctrl-C Erik Zeitler

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

More Related