1 / 14

Arrays – Creating Them

Arrays – Creating Them. Many ways to create arrays (that's BOTH vectors and matrices…). Methods to create arrays. _________________________________ _________________________________ _________________________________ _______________  _________________ ones(), zeros(), rand(), eye() … etc …

kaya
Download Presentation

Arrays – Creating Them

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 – Creating Them Many ways to create arrays (that's BOTH vectors and matrices…)

  2. Methods to create arrays • _________________________________ • _________________________________ • _________________________________ • _______________ _________________ ones(), zeros(), rand(), eye() … etc… • __________________________________ (using loops) • range operator (the colon : ) • upload data from a file (.txt, .xls…)

  3. 1. Hardcoding values in arrays

  4. Other symbols useful to hardcode • [] are used to hardcode values into an array • , (commas) are optional, but they separate COLUMNS • ; (semi-colons) are necessary if you want to hardcode a new row • ’ (apostrophe) are used to transpose a vector/matrix • “Flip it”: 1st column becomes 1st row, 2nd column becomes 2nd row… • : (colons) are useful when the values have an “addition-pattern”

  5. 1.b. Hardcoding arrays space (or commas) create columns semi-colons create new rows Combine both to create a matrix! 1 rule only! AT ALL TIME AN ARRAY MUST BE RECTANGULAR! NO HOLES!!!

  6. 2. Using pre-existing variables • Drawing the castle • Drag Calculations (speed intersection)

  7. 2.b Use pre-existing arrays to create arrays • We've used scalars to create vectors • We can use vectors to create matrices • We can use matrices to create matrices! >> v1 = [2 4 5.5] v1 = 2.0000 4.0000 5.5000 >> v2 = [-6 -9 4.2] v2 = -6.0000 -9.0000 4.2000 >> >> matrix = [v1; v2] matrix = 2.0000 4.0000 5.5000 -6.0000 -9.0000 4.2000

  8. 3. Using equations • Truss

  9. 4. Using functions • linspace()

  10. 4. Continued: MORE FUNCTIONS! • More functions exist to help create arrays (vectors AND matrices): • ones() creates an array full of 1's • zeros() creates an array full of 0's • rand() creates an array full of 0< floats <1 • In all cases, MATLAB requires arguments: IN ORDER: • 1st: number of rows, 2nd: numbers of columns, 3rd: number of layers, etc.. • If only 1 argument is provided, MATLAB uses that value for both number of rows AND number of columns.

  11. 6. Using the colon operator • Another operator can be used to create vectors (then matrices) with addition pattern. For examples: v1 = ____________________________ v2 = ____________________________

  12. 7. Last operator: the apostrophe • The apostrophe allows to transpose a vector • rows will become columns • (therefore columns will become rows..) x = 14 17 16 17 16 16 16 16 18 18 11 18 17 14 12 11 >> y = x' y = 14 18 17 18 16 11 17 18 16 17 16 14 16 12 16 11 >>z = y' z = 14 17 16 17 16 16 16 16 18 18 11 18 17 14 12 11

  13. Definition of the symbols • [] are used to hardcode values into an array • , (commas) are optional, but they separate COLUMNS • ; (semi-colons) are necessary if you want to hardcode a new row • ’ (apostrophe) are used to transpose a vector/matrix • “Flip it”: 1st column becomes 1st row, 2nd column becomes 2nd row… • : (colons) are useful when the values have an “addition-pattern”

  14. Key Points • CAUTION: at this time, the only type of arrays we have seen are vectors and matrices. These do NOT allow to mix data types. Only numerical values or char values within one array. • Square-brackets[]commas , semi-colons ; colons:and apostrophes ’ are all used to hardcode array. • Prefer these symbols when arrays are small, NOT when arrays are big. • Exception: the colon can technically create big arrays instantly. • Combine any symbols, as long as the array created is rectangular!

More Related