130 likes | 245 Vues
Arrays (part2). Creating arrays Referencing arrays Traversing arrays Even more built-in functions Slicing. more powerful keywords. Built-in functions. Functions designed for arrays.
E N D
Arrays (part2) Creating arrays Referencing arrays Traversing arrays Even more built-in functions Slicing
more powerful keywords Built-in functions
Functions designed for arrays • Since MATLAB was built for MATRICES (and all other simplified matrices, i.e. vectors…), LOTS of functions are available!!!!
Results differs based on argument size Argument: vector Argument: matrix minimum of EACH column! • minimum of entire matrix? • res = ____________________
Math functions: sum() and prod() Argument: vector Argument: matrix sum of EACH column! • sum of entire matrix? • res = ____________________
Remember exam2 parking lots? • Running totals were used to count: • Spots Open, and • Total Spots What if…. we didn't need running totals… Worksheet
The best of the best… Slicing
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!)
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..)..
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!!
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!!!!!
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
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