1 / 20

The MatLab Language

The MatLab Language. Mathematics Laboratory The MathWorks www.mathworks.com. General. Comments – “%” in first column Result Control - “;” at the end "x=5" x = 5 "x=7;“ "x" x = 7. General (continued). Range Operator "x=3:2:13" x = 3 5 7 9 11 13

gammons
Download Presentation

The MatLab Language

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. The MatLab Language Mathematics Laboratory The MathWorks www.mathworks.com

  2. General • Comments – “%” in first column • Result Control - “;” at the end • "x=5" x = 5 • "x=7;“ • "x" x = 7

  3. General (continued) • Range Operator • "x=3:2:13" x = 3 5 7 9 11 13 • Line Continuation - three periods • Directory • "dir" announ~2.doc mathview.doc matlab_6.doc matlab~2.doc intro.m matlab.doc matlab~1.doc vector.m announ~1.doc language.m matlab1.doc matlab~1.ppt

  4. General (continued) • Print Working Directory • "pwd" ans = E:\DOC\JEFF\COURSES\BEI\SHORTC~1\MATLAB • Change Directory - cd • "cd .." • "pwd" ans = E:\DOC\JEFF\COURSES\BEI\SHORTC~1

  5. General (continued) • "cd matlab" • "pwd" ans = E:\DOC\JEFF\COURSES\BEI\SHORTC~1\MATLAB

  6. Scalars, Vectors, and Matrices • Creating a row vector • "X=[1,2,3] or X=[1 2 3] X = 1 2 3 • Creating a column vector • "Y=[1;2;3]" or "Y=[1 2 3]" Y = 1 2 3

  7. Vector / Matrix Operations • "Z=[y,2*y,3*y]" creates ? • Z = 1 2 3 2 4 6 3 6 9

  8. Indexing • "Z(2,3)=?" ans = 6 • "W=Z(1:2,2:3)" W = 2 3 4 6 • "W=Z(:,2)" W = 2 4 6

  9. Indexing (Continued) • "W=Z(2,:)" W = 2 4 6 • "Z(2,:)=[6,4,2] does ?" Z = 1 2 3 6 4 2 3 6 9 • "Z(2,:)=[ ] does ?" Z = 1 2 3 3 6 9

  10. Matrix Operations • A+B - Addition, element by element • "ZZ=[1,1,1;2,2,2]" ZZ = 1 1 1 2 2 2 • "W=Z+ZZ" W = 2 3 4 5 8 11

  11. Matrix Operations (Continued) • A' - Transpose • A*B - Matrix Multiplication • "ZZ=ZZ' " ZZ = 1 2 1 2 1 2 • "W=Z*ZZ" W = 6 12 18 36

  12. Matrix Operations (Continued) • A.*B - Element by Element Multiply • Also: A/B, A\B, A./B, A.^2 • Reserved Symbols • Scalars: pi, i, j, inf, NaN, clock, date, ans • "x=pi" x = 3.1416 • "x=i" x = 0 + 1.0000i

  13. Scalars (Continued) • "x=j" x = 0 + 1.0000i

  14. Matrices: zeros, ones, eye • "zeros(3,5)" ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

  15. Matrices (Continued) • "ones(5,3)" ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

  16. Matrices (Continued) • "eye(4)" - the identity matrix ans = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1

  17. Relational Operators • "<" - less than • "<=" - less than or equal to • ">" - greater than • ">=" - greater than or equal to • "==" - test for equality • "~=" - not equal to • "&" - AND • "|" - OR • "~" - NOT

  18. Control Flow (If) if x>5 & x<8 ------- ------- elseif(x>=8) ------- ------- else ------- end

  19. Control Flow (For Loops) for k=7:21 ------- ------- end

  20. Control Flow (While Loops) k=1; while k=<10 ------- ------- k=k+1; end

More Related