1 / 18

Matlab Arrays

Matlab Arrays. Vectors. Initialize vectors either like : V = [1 2 3 4 5], or : V = [1,2,3,4,5] In order to create a column vector : V = [1;2;3;4;5] You can put any numeric expression for the individual elements of your vector. Accessing Vector Elements.

knightm
Download Presentation

Matlab Arrays

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. Matlab Arrays

  2. Vectors • Initialize vectors either like : • V = [1 2 3 4 5], or : • V = [1,2,3,4,5] • In order to create a column vector : • V = [1;2;3;4;5] • You can put any numeric expression for the individual elements of your vector

  3. Accessing Vector Elements • Given v = [1 2 3 4 5], or v = [1;2;3;4;5] • V(1) returns 1, • V(2)  2, … v(5)  5 • v(6)  ??? Index exceeds matrix dimensions. • v(0)  ??? Subscript indices must either be real positive integers or logicals.

  4. Changing Vector Elements >> v(3) = 8 v = 1 2 8 4 5 >> v(0) = 0 ??? Subscript indices must either be real positive integers or logicals. >> v(7) = 128 v = 1 2 8 4 5 0 128

  5. Strings are also vectors … >> st = 'abcde' st =abcde >> st(0) ??? Subscript indices must either be real positive integers or logicals. >> st(3) ans =c >> st(3) = 'X' st =abXde >> st(10) = 'O' st =abXde O

  6. Transpose Operator >> [1 2 3]' ans = 1 2 3 >> [1;2;3]' ans = 1 2 3

  7. Vector length >> length(v) ans = 7 >> length(v') ans = 7 >> v(length(v)) ans = 128 >> v(end) ans = 128

  8. Vector operations • Given vector v = [1 2 3 4 5] >> v + 10 ans = 11 12 13 14 15 >> v * 2 ans = 2 4 6 8 10 >> v' + 10 ans = 11 12 13 14 15

  9. Vector operations • Given vectors v = [1 2 3 4 5], v2 = [10 20 30 40 50] : >> v + v2 ans = 11 22 33 44 55 >> v2 -v ans = 9 18 27 36 45 >> v + v2' ??? Error using ==> + Matrix dimensions must agree.

  10. Vector Operations >> v * v2 ??? Error using ==> * Inner matrix dimensions must agree. >> v / v2 ans = 0.1000 >> v(3) = 6 v = 1 2 6 4 5 >> v / v2 ans = 0.1164

  11. Element-by-element operations >> v .* v2 ans = 10 40 90 160 250 >> v ./ v2 ans = 0.1000 0.1000 0.1000 0.1000 0.1000 >> v .+ v2 ??? v .+ v2 | Error: "identifier" expected, "+" found. >> v + v2 ans = 11 22 33 44 55

  12. Functions that Accept Vectors >> log(v) ans = 0 0.6931 1.0986 1.3863 1.6094 >> sin(v) ans = 0.8415 0.9093 0.1411 -0.7568 -0.9589 >> round(10 * log(v)) ans = 0 7 11 14 16

  13. Creating Vectors by Ranges >>v = 1:5 v = 1 2 3 4 5 >> v = 11:16 v = 11 12 13 14 15 16 >> v = 5:1 v = Empty matrix: 1-by-0 • in general : first:last  [first, first+1, first+2, … ,last-1, last]

  14. Specifying an Increment >> v = 10:10:50 v = 10 20 30 40 50 >> v = 50:-10:10 v = 50 40 30 20 10 >> v = 10:-10:50 v = Empty matrix: 1-by-0 >> v = 10:20:50 v = 10 30 50 >> v = 10:15:50 v = 10 25 40

  15. Linspace function • LINSPACE(X1, X2) generates a row vector of 100 linearly equally spaced points between X1 and X2. • LINSPACE(X1, X2, N) generates N points between X1 and X2. >> linspace(10, 20, 5) ans = 10.0000 12.5000 15.0000 17.5000 20.0000

  16. Plotting >> x = linspace(0, 4 * pi, 100); >> y = sin(x); >> y2 = (sin(x)) .^ 2; >> y3 = y + y2; >> plot(x,y);

  17. Plotting … >> plot(x,y2); >> plot(x,y3);

  18. Multiple Plots >> plot(x,y, x, y2, x, y3); >> title('An interesting plot'); >> xlabel('x'); >> ylabel('y'); >> legend('sin(x)', 'sin(x)^2', 'sin(x) + sin(x)^2');

More Related