1 / 49

Arrays – the Basics

Arrays – the Basics. Arrays Scalar Vectors Matrices. 1. 1. An array. An array is a collection of data into one variable The array has a more precise name , depending on the dimension of the variable: A “scalar” A “vector” A “matrix”. The difference…. 2. 1 st . “A scalar ”.

kevork
Download Presentation

Arrays – the Basics

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. Arrays – the Basics Arrays Scalar Vectors Matrices 1

  2. 1. An array • An array is a collection of data into one variable • The array has a more precise name, depending on the dimension of the variable: • A “scalar” • A “vector” • A “matrix” The difference… 2

  3. 1st . “A scalar” • A value, just one. • The dimension is 1 x 1. • Example: the number 7 is a scalar. age = 17; %creates a scalar variable weight = 145.32; %another scalar variable 3

  4. 2nd . “Vector” – math In 2D: 4

  5. 2nd . “Vector” – math In 2D: In 3D: The vector OP has initial point at the origin O(0,0,0) and terminal point at P(2,3,5). We can draw the vector OP as follows: Source: http://www.intmath.com/Vectors/1_Vector-concepts.php In 3D: 5

  6. “Vector” – MATLAB • A vector is a one-dimensional collection of data • “all the temperatures readings during take-off” • “all the pressures recorded during Hurricane Katrina” • “an (x,y,z) coordinate” 6

  7. “Vector” – Matlab • A vector is a one-dimensional collection of data • “all the temperatures readings during take-off” • “all the pressures recorded during Hurricane Katrina” • “an (x,y,z) coordinate” • A vector can only be of two types: a column, or a row • For an array to be a vector, only ONE of this variable’s dimension is 1. A column vector (“m by 1”) A row vector (“1 by m”) 7

  8. Vectors, end. • Vectors are usually thought of as “row vectors” or “column vectors”, but there exists one more: The “empty vector” – a variable in MATLAB that contains no values. To make an empty vector, use the empty brackets: E = [];

  9. Vectors, end. • Vectors are usually thought of as “row vectors” or “column vector”, but there exists one more: The “empty vector” – a variable in MATLAB that contains no values. To make an empty vector, use the empty brackets: E = []; • Why do this? • Create a variable, much like a container ready to accept values. • Erase the previous content of an array, without destroying the variable itself.

  10. 3rd . A “matrix” • Both dimensions are greater than 1. • Can be used to: • Represent mathematical matrices, or • Simply store information • First row: temperature of different points in a nozzle • Second row: pressure at those same points • Third row: density at those same points an “r by c” 10

  11. 3rd . A “matrix”, vocabulary • LEARN this NOW – everything depends on you referring to these correctly! 1 2 3 4 5 6 7 8 9 0 3 1 5 6 1 4 3 5 6 7 3 1 2 0 0 ROW!

  12. 3rd . A “matrix”, vocabulary • LEARN this NOW – everything depends on you referring to these correctly! 1 2 3 4 5 6 7 8 9 0 3 1 5 6 1 4 3 5 6 7 3 1 2 0 0 COLUMN!

  13. Creating Arrays Creating scalars, vectors, matrices Ex. Plotting Graphs Ex. Conversion Table Ex. Plotting functions Ex. Use of matrices in real world 13

  14. 1. Creating scalars • Assigna value to a variable (i.e. Hardcode) pressure = 10; %pascals temperature = 298; %kelvin • Store the result of an equation pressure = density*R*temperature; • Save the return-value of the input() command age = input(‘Enter your age: ’); 14

  15. 2. Creating vectors • There are LOTS of ways to create vectors, based on three simple ideas: • The values in the vector are pre-defined. For example: [ 2 -5 4.4 -96.6] 15

  16. 2. Creating vectors • There are LOTS of ways to create vectors, based on three simple ideas: • The values in the vector are pre-defined. For example: [ 2 -5 4.4 -96.6] • The values have a pattern (addition only). For example: [10, 20, 30 ,…100] or [-10 -8 -6 -4 -2 0] 16

  17. 2. Creating vectors • There are LOTS of ways to create vectors, based on three simple ideas: • The values in the vector are pre-defined. For example: [ 2 -5 4.4 -96.6] • The values have a pattern (addition only). For example: [10, 20, 30 ,…100] or [-10 -8 -6 -4 -2 0] • Finally, the total amount of values is known. For example: 25 points evenly spaced from 0 to 100. 17

  18. 2.1. Pre-defined values 18

  19. 2.1. Pre-defined values, cont. 19

  20. 2.1. Pre-defined values, cont. 20

  21. 2.1. Pre-defined values, cont. What else are semi-colons used for? 21

  22. 2.1. Pre-defined values, cont. They create rows AND suppress output! What else are semi-colons used for? 22

  23. 2.1. Pre-defined values, cont. The apostrophe allows to transpose a vector. Rows become columns. Columns become rows. They create rows AND suppress output! What else are semi-colons used for? 23

  24. 2.1. Pre-defined values, cont. The apostrophe allows to transpose a vector. Rows become columns. Columns become rows. They create rows AND suppress output! What dimension will speeds have? _______________________________ What else are semi-colons used for? 24

  25. y x y -7 -2 3 8 4 -7 3 -1 x Ex. Plotting graphs • In order to plot, Matlab needs data points:

  26. y x y -7 -2 3 8 4 -7 3 -1 x Ex. Plotting graphs • In order to plot, Matlab needs data points:

  27. y x y -7 -2 3 8 4 -7 3 -1 x Ex. Plotting graphs • In order to plot, Matlab needs data points: Matlab connects the dots!

  28. y x y -7 -2 3 8 4 -7 3 -1 x Ex. Plotting graphs • In order to plot, Matlab needs data points: • Well… • x is an array of data points x = [-7 -2 3 8] • y is another array of data points y = [4 -7 3 -1] • …for the curious ones, to plot: plot(x,y)

  29. 2.2. Patterns (addition only) The range operator Numbers are separated by +1 29

  30. 2.2. Patterns, cont. The range operator Numbers are separated by +1 An additional value in the middle specifies the increment. +3 +3 +3 +3 +3 +3 +3 +3 >32  30

  31. 2.2. Patterns, cont. The range operator Numbers are separated by +1 An additional value in the middle specifies the increment. Go reverse by using a negative increment! CAUTION: the beginning number must be > the end number. Here 10>3. (This also shows it works with decimals.) +3 +3 +3 +3 +3 +3 +3 +3 >32  -2.5 -2.5 -2.5 < 3  31

  32. 2.2. Patterns, cont. The range operator Numbers are separated by +1 An additional value in the middle specifies the increment. To use the apostrophe and create a column vector, absolutely place brackets first! … else…. +3 +3 +3 +3 +3 +3 +3 +3 >32  -2.5 -2.5 -2.5 < 3  32

  33. 2.2. Patterns, cont. The range operator Numbers are separated by +1 An additional value in the middle specifies the increment. To use the apostrophe and create a column vector, absolutely place brackets first! … else…. Only the scalar -10 gets transposed: but a scalar transposed remains the same scalar! +3 +3 +3 +3 +3 +3 +3 +3 >32  -2.5 -2.5 -2.5 < 3  33

  34. Ex. Conversion table % create celsius data points celsius = 0:10:100; %0 to 100 by +10 increment % calculate Fahrenheit fahrenheit = celsius * 9/5 + 32; % show table <code not shown>

  35. 2.3. Specific amount of data points • A built-in function called linspace() spaces elements linearly in an array. • What does this mean? • The distance between each consecutive data point is equal. • There are two ways to use it, as Matlab ‘hints’ when the command typed is unfinished: Either provide 2 arguments, or provide 3 arguments. 35

  36. 2.3. linspace(), cont. The third argument indicates the ________________________ . 36

  37. 2.3. linspace(), cont. The third argument indicates the ________________________ . When Matlab cannot display all the elements on one line, it simply indicates the column-number per line. 37

  38. 2.3. linspace(), cont. The third argument indicates the ________________________ . When Matlab cannot display all the elements on one line, it simply indicates the column-number per line. 38

  39. 2.3. linspace(), cont. ?????? %no third argument Omitthe third argument uses a default of _______ data points!

  40. 3. Creating Matrices • Simply a combinationof all symbols introduced with vectors! • Square brackets [ ] • Spaces or commas , , • Semi-colons ; • Apostrophes ’ 40

  41. 3.1. Matrices: hard-coding • Use semi-colons to create new rows. • ONLY rectangular matrices: • The number of columns MUST match for each row, and vice-versa. 41

  42. 3.2. Reusing Previous matrices • Use semi-colons to create new rows. • ONLY rectangular matrices: • The number of columns MUST match for each row, and vice-versa. Use previousmatrices to actually create new matrices. This example transposes the matrix variable a. 42

  43. 3.3. Using Colons • Use semi-colons to create new rows. • ONLY rectangular matrices: • The number of columns MUST match for each row, and vice-versa. You can use previousmatrices to actually create new matrices. This example transposes the variable a. Combine any previous methods, AS LONG AS the matrix remains rectangular. 43

  44. 3.4. “Concatenating” • Use semi-colons to create new rows. • ONLY rectangular matrices: • The number of columns MUST match for each row, and vice-versa. Finally, create arrays by combining previous variables! This is called CONCATENATING. You can use previousmatrices to actually create new matrices. This example transposes the variable a. You can combine any previous methods, AS LONG AS the matrix remains rectangular. 44

  45. 3.5. Using the command window • Use semi-colons to create new rows. • ONLY rectangular matrices: • The number of columns MUST match for each row, and vice-versa. When the array becomes too big, the numbers no longer display. You can use previousmatrices to actually create new matrices. This example transposes the variable a. You can combine any previous methods, AS LONG AS the matrix remains rectangular. 45

  46. Ex. Conversion table, end! % create celsius data points celsius = 0:10:100; %0 to 100 by +10 increment % calculate Fahrenheit fahrenheit = celsius * 9/5 + 32; % show table [celsius’ fahrenheit’]

  47. Ex. Sling Thermometer A method to read relative-humidity.

  48. Ex. Images Each row and column have a pixel value stored.

  49. Making Arrays We’ve just begun arrays – much more to follow Since an array frequently holds a large number of values, loops are indispensable. Matrices typically involve nested loops Next time and then some: using arrays (“referencing” them) changing arrays learning the MATLAB array tools

More Related