Download Arrays in Matlab: Creating, Accessing, and Manipulating - Prof. David Smith and more Study notes Computer Science in PDF only on Docsity! Arrays Arrays vs. Vectors All vectors are just 1 row arrays Arrays also homogenous collections Every value must be the same Arrays must always be rectangular You can never have a jagged array If you try to make one, matlab will usually error or fill-in zeros. Creating Arrays Direct-Access Use semi-colons to separate rows. Each row must be the same length. Functions Linspace and colon can't make arrays ones, zeros, rand can Accessing-Indexing Emphasize index row then column Show indexing still done in parentheses, but we separate each dimension by a comma arr(r, c) Single indexing Same rules apply, must be positive integers in the range arr(2, 7) arr(4, 8) row then column Shorthand operators End end is in relation to the position arr(end, 1) - end wil be the value of the last row arr(1, end) - end will be the value of the last column arr(end, end) - is fine, each end will just be the value for that dimensin end makes no context outside of indexing. If you want dimensions, use size() Colon (:) colon means all of that dimension arr(1, :) - first row, all columns arr(:, 1) - all rows, first column arr(:, :) - all rows, all columns, equivalent to just doing arr Multiple Indices - IMPORTANT!!!! Matlab returns the intersection of the rows you want and the columns you want arr(rows_want, cols_want) rows_want - a vector of row indices cols_want - a vector of col indices The output array will be dimensions length(rows_want)*length(cols_want) Example arr([1 3 5], [2 4]) Output is a 3x2. Will have the 1, 3, 5 rows, but only with the values at the columns 2, 4 Setting positions Same as vectors, except now specify rows and columns specify the spots you want to set on the left-hand side, and what you want in those spots on the right hand-side IMPORTANT. Spots you specify and the values you put in those spots must be same dimension, or the value must be scalar Deleting elements Can only delete rows or columns, else the array will become jagged arr(1, :) = [] -> deletes first row arr(:, 3) = [] -> deletes 3rd column arr(2:4, :) = [] -> deletest rows 2 and 4 ERRORS arr(3, 2) = []; arr(3, 1:end) = []; Must use : to delete Linearized Indexing giving one position is not an error arr(7) is ok. When doing linear indexing, the indices are found by counting down the columns. To linearize an array to a column vector, just do arr(:). If you want that to be a row vector, just transpose, or arr(1:end) When array is linearized, it just goes down the columns. Indexing with one specification always leads to a vector vec(1:3:5) -> vector arr(1:3:5) -> vector Slicing top/bottom halves -> arr(1:end/2, :), arr(end/2+1:end quarters odd rows/columns, even rows/columns Reversing rows/columns Logical Indexing arrays The masking principle. You are basically overlaying another array/vector, of trues and false, and the only spotsthat come out is where there were trues. To find the elements greater than 4 arr(arr > 4) No need to specify rows and columns with logicals. arr > 4 gives you a logical ARRAY that overlays on top of the original If you do b = arr(arr > 4), output always a vector