1 / 13

Arrays (part2)

Arrays (part2). Creating arrays Referencing arrays Traversing arrays Even more built-in functions Slicing. more powerful keywords. Built-in functions. Functions designed for arrays.

yelena
Télécharger la présentation

Arrays (part2)

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 (part2) Creating arrays Referencing arrays Traversing arrays Even more built-in functions Slicing

  2. more powerful keywords Built-in functions

  3. Functions designed for arrays • Since MATLAB was built for MATRICES (and all other simplified matrices, i.e. vectors…), LOTS of functions are available!!!!

  4. Results differs based on argument size Argument: vector Argument: matrix minimum of EACH column! • minimum of entire matrix? • res = ____________________

  5. Math functions: sum() and prod() Argument: vector Argument: matrix sum of EACH column! • sum of entire matrix? • res = ____________________

  6. Remember exam2 parking lots? • Running totals were used to count: • Spots Open, and • Total Spots What if…. we didn't need running totals… Worksheet

  7. The best of the best… Slicing

  8. Slicing • Slicing: look at specific pieces of an array (specific elements) • Why? • Replace pieces by others • Interchange pieces/ swap • Remove pieces (some rules to follow) • Copy pieces into another variable (extract and copy) • CAUTION: not necessarily “delete”… • Rules: • When removing: Don’t make an array non-rectangular!! • When replacing: replace by the same amount of pieces..(back to rule 1) • Operators used: • Brackets, colons, (commas), the keyword end (black!)

  9. Operators Used • Colons: specifies a range 2:4 ("2 to 4 by +1") • Colon alone! :("ALL") • Brackets: slice non-consecutive data [3 7 4 ] ("3rd then 7th then 4th") • The keyword end (black!) "go to the end", "last" Examples (for simplicity, v indicates a vector, mindicates a matrix) x = v(2:4); %copy the 2nd to 4th element into x v([3 7 4]) = 5;%replace the 3rd then 7th then 4th element by 5 v([3 7 4]) = [4 6 2]; %replace the 3rd then 7th then 4th by 4,6,2 x = v(4:end);%copy the 4thto last element into x x = v(2:3:end); %copy every 3rd element from the 2nd to the last.. x = v(end:-1:3); %copy from last to 3rd (reverse order..)..

  10. Operators used • Examples (for simplicity, v indicates a vector, m indicates a matrix) x = m( ____ , _____);%rows FIRST comma columns SECOND x = m(3,4);%copy the 3rd row, 4th column (tiny slice…) x = m(3:5 , 4); %copy the 3rd 4th and 5th row, of 4thcolumn x = m(: , [4 9]); %copy all rows, of 4ththen 9th columns x = m([2 7 3] , end); %copy the 2nd then 7th then 3rd rows, last column x = m([1,end],[1 end]); %copy the 4 corners!!

  11. DELETING __________ = []; %EQUALS EMPTY BRACKETS This deletes whatever you reference on the left. THIS CANNOT BE UNDONE. MATLAB IMMEDIATELY RELEASES THAT MEMORY. v(2:4) = [];%delete the 2nd to 4thelement v([3 7 4]) = [];% delete the 3rd then 7th then 4th v(4:end) = [];% delete the 4th to last element v(2:3:end) = [];% delete every 3rd element from the 2nd to the last.. Deleting from vectors is safe… but be careful with matrices!!!!!

  12. CAUTION !!!! DELETING FROM MATRICES • Rule: ______________________________________________ m(3,4) = []; good? bad? m(3:5 , 4) = []; good? bad? m(: , [4 9]) = []; good? bad? m([2 7 3] , end) = []; good? bad? m([1,end],[1 end]) = []; good? bad? m(:,[1 end]) = []; good? bad? Worksheet

  13. Key Points • Built-in functions: min() max() mean() sort() sum() prod()… • 2 results possible: one for vectors, one for matrices • Slicing means REFERENCING A PIECE(S) • instead of a set index, specify: • a range using the : colon operator • specific sets of indices: use [ ] • the keyword end • CAUTION when deleting: ____ = []; • array must remain rectangular

More Related