Arrays ------------------------------------------------------------------------------- (1) Arrays and Matrices + MATLAB's law: - "everything is an array" - sometimes we say MATRIX + array vs matrix? - ARRAY: collection of identically typed data that can be accessed by indicies - MATRIX: special kind of array (see matrices.txt) ------------------------------------------------------------------------------- (2) No Values + Empty array: [] + contains no values + handy way to initialize an array variable! >> x = [] + related function: ISEMPTY ------------------------------------------------------------------------------- (3) Scalar values SCALAR: single value + may also think of as 1 row and col or "0-dimensional" array + pretty much what we've been using so far! >> 1, [1], [[1]] % MATLAB will remove redundant []s Operations: + see HELP OPS + Use "unary operations" ------------------------------------------------------------------------------- (4) One-Dimension + 1-D array: - one row or one column - MATLAB stores information as rows, usually - sometimes we say VECTOR + Notation: - to separate individual elements, use spaces or commas - to separate individual rows, use semicolons >> [1 2 3] % 1-D array (row) >> [1;2;3] % 1-D array (col) + Transpose: convert row to col with $'$ >> x = [1 2 3]; y=x' + Built in ways to generate: - colon: >> 1:5 >> 0:2:6 >> 0:2.1:6 >> 6:-2:0 - linspace + LINSPACE(x1, x2) generates a row vector of 100 linearly equally spaced points between x1 and x2. + LINSPACE(x1, x2, N) generates N points between x1 and x2. >> linspace(1,10,13) + colon vs linspace? - colon for knowing the increment! - linspace for not needing to worry about increments + Operations: - discussed along with arrays - there are vector ops,too! dot product (DOT), cross product (CROSS) (and more) ------------------------------------------------------------------------------- (4) Two-Dimension + 2D arrays: - multiple rows and columns - real-life examples include tabular data and spreadsheets - always rectangular! no ragged arrays in MATLAB (unless you use something called a CELL ARRAY) - more than 2D? Yes -- called MULTIDIMENSIONAL + Row major: - build 1 row at a time - MATLAB's "default" way of building arrays - "extra" []s are condensed as with scalars - rectangularity must be preserved rows: spaces/commas separate elements cols: semicolons separate rows (say $;$ as "start new row") >> [1 2; 3 4] % square matrix with rows [1 2] and [3 4] >> [1:3; 4:6] >> [1,2; 3] + appending arrays: - treat item in [a1, a2, ...] as an element - then remove all but the outer brackets, as in [[a1,a2,...],[a3,a4,...]] >> A=[1 2; 3 4]; >> [A,A] % creates an array of 4 columns and two rows + Column-major? 2 tricks: (1) Transpose: - swaps rows with columns - means that Aij becomes Aji for all combinations of i and j - use transpose operator $'$ >> A = [1 2; 3 4]; >> A' >> r1 = [1 3]; r2 = [2 4]; >> [r1' r2'] (2) Single indexing - A(index) converts the entire array into one column array, where the elements are arranged column-by-column - very obscure syntax that few know. DIS loves this kind of detail >> A=[1 2; 3 4]; >> A(3) % hint: MATLAB index starts from 1 and think columns.... ------------------------------------------------------------------------------- (5) Multidimensional array + you can have more than 2D + 3D is a box + > 3D is a "hyperbox" + we won't do too much with these.... ------------------------------------------------------------------------------- (6) Indexing + Index: - numerical location of an element in an array - MATLAB INDEXS STARTS FROM 1, NOT 0!!!!! + How many indicies? indexing based on dimension: OD: none scalar 1D: one row or col depending on type of array 2D: two row and col in order (row,col) 3D: three (row,col,page) + Math notation: A ijk.... 1st index (i) is 1st dimension (row) 2nd index (j) is 2nd dimension (col), 3rd index (k) is 3rd dimension (page) and so on.... + MATLAB syntax: A(i,j,k,....) to index/extract an element an index may be in the form of i1:i2 to index a subarray of elements see HELP PAREN for more information ------------------------------------------------------------------------------- (7) Extracting Elements + Single element extraction: 1D row: name(col) 1D col: name(row) 2D row major: name(row,col) 2D col major: name(pos) >> x = [1:10]; >> x(5) >> A = [1 2; 3 4]; >> A(1,2) % get element at row 1, col 2 + Multiple element extraction: - all elements: >> A = [1 2; 3 4]; >> A % default behavior >> A(:,:) % get all elements of A in "regular" order (redundant!) >> A(:) % get all elements of A in column major order - subarrays (portion of the array): + use vector of indicies (a 1-D array of indicies) 1D: name(vector) 2D: name(vector,vector) 2D: name(vector) + MATLAB will output the subarray in the order in which you write the indicies! >> x = [1:4]; >> x([1 3]) >> A = [1 2 3 ; 4 5 6]; >> A([1 2],2) % get the second column >> A(1, [3 2 1]) % for row 1, get elements at cols 3, 2, and 1 >> A([1 2],[1 3]) % get elems (1,1), (1,3), (2,1), (2,3) % outputs in this order: row1: col1 col3 % row2: col1 col3 + Using the colon for subarrays (see HELP COLON): - instead of vectors like [1 2 3 4], use 1:4 - if you know that 4 is the end of the array, you could use 1:end - when you see a colon in an index, it means "for all of the...." >> A = [1 2; 3 4]; >> A(:,:) % extract all rows and all cols >> A(:,1) % for all rows of A, extract the elem from the 1st column % shorter: get 1st column from A >> A(1,:) % for all cols of A, extract the first (or "top") elem % for the 1st row of A, extract all cols % even shorter: get 1st row of A >> B = rand(5,4); >> B([1 2 3],:) % for all cols, get 1st thru 3rd row elements % extract first three rows >> B([3 2 1],:) % extract 3rd, 2nd, 1st elements from each col % extract first three rows in reverse order >> B(:,[4 1]) % for all rows, extract 4th and 1st elements % extract 4th and 1st columns >> B([2 5],[4 2]) % extract elems with locs (2,4) (2,2) (5,4) (5,2) % extract subarray >> B([1 2 4],[3 1]) % from rows 1 2 4, extract cols 3 1 ------------------------------------------------------------------------------- (8) Insertion + Insertion: - put elements INTO an array or exchange values - how to think about it: "LHS gets RHS in terms of var=expression" - so, the places on the left get the values on the right - so, you need to work out the right side before doing the left side example) >> A = [1 2; 3 4] >> A(1,1) = 17 example) >> A=[1:3; 4:6]; >> A([1 2],:) = A([2 1],:) would be interpreted as the new row 1 of A gets the old row 2 of A the new row 1 of A gets the old row 1 of A or, "swap rows of A" + you can insert "beyond" the given dimensions! MATLAB fills in needed "spots" with zeros >> A=[1 2; 3 4]; >> A(6,6) = 10 ------------------------------------------------------------------------------- (9) Builtin Arrays + scalar values - all real nums between -REALMAX and REALMAX - all "tiny" values above REALMIN + empty array [] + Initial values: ZEROS generate an array of zeros ONES generate an array of ones RAND generate an array of random numbers between (0,1) (does not include 0 and 1) >> zeros >> zeros(1,2) >> zeros(2) see also EYE and DIAG ------------------------------------------------------------------------------- (10) Useful functions and builtin values for arrays: + SIZE: >> A = [1:3; 4:6]; >> [row col] = size(A) + LENGTH: >> length(A) + END: >> A(2,1:end) ------------------------------------------------------------------------------- (11) Operations + rules: - happens one element at a time unlike matrices! - see HELP ARITH, HELP OPS + addition: $+$ and $-$ work element by element Aij+Bij=Cij >> [1 2] + [3 4] % works for both matrices and arrays + multiplication: * and / are a bit different! use $.*$ and $./$ to get element-by-element mul and div >> [1 2] .* [3 4] % array multiplication >> [1 2] * [3 4]' % matrix multiplication (dot product) + logic: compare one element at a time! see logicalarrays.txt for more info >> A=[1 2; 3 4]; >> A==2 ------------------------------------------------------------------------------- (12) Functions + rules: - functions generally accept arrays as well as scalars - for array input, expect array "output" >> sqrt(4) >> sqrt([1 4 16]) >> plot(1:10) -------------------------------------------------------------------------------