140 likes | 276 Views
Arrays – Creating Them. Many ways to create arrays (that's BOTH vectors and matrices…). Methods to create arrays. _________________________________ _________________________________ _________________________________ _______________ _________________ ones(), zeros(), rand(), eye() … etc …
E N D
Arrays – Creating Them Many ways to create arrays (that's BOTH vectors and matrices…)
Methods to create arrays • _________________________________ • _________________________________ • _________________________________ • _______________ _________________ ones(), zeros(), rand(), eye() … etc… • __________________________________ (using loops) • range operator (the colon : ) • upload data from a file (.txt, .xls…)
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”
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!!!
2. Using pre-existing variables • Drawing the castle • Drag Calculations (speed intersection)
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
3. Using equations • Truss
4. Using functions • linspace()
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.
6. Using the colon operator • Another operator can be used to create vectors (then matrices) with addition pattern. For examples: v1 = ____________________________ v2 = ____________________________
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
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”
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!