CS100J, Fall 2001 Lecture 25 Thurs 11/29 ------------------------------------------------------------------------------- Announcements: + P6 due 12/4 + MATLAB portion has an extension (due 12/6) as an exercise (I want you to learn a bit more MATLAB first!) + Comprehensive makeup on 12/6 -- contact Beth Howard to get your name on the list if you didn't contact Laurie Buck already (time and location currently unknown) + 12/5 (Weds) will be special OOP review session in the evening + there will be another review session before the final + remaining topics: MATLAB until 12/6, then $args$ and course summary ------------------------------------------------------------------------------- Topics: + numerical arrays + matrices + operations and functions + indexing and inserting + logical arrays ------------------------------------------------------------------------------- MATLAB's law: + "everything is an array" + sometimes we say MATRIX - an array is a more general quantity - MATLAB classifies some operations as array and others as matrix ------------------------------------------------------------------------------- Scalar values + 1 row and col or "0-dimensional" array >> 1, [1], [[1]] % MATLAB will remove redundant []s ------------------------------------------------------------------------------- One-Dimension + one row or one column + MATLAB stores information as rows, usually - 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) ------------------------------------------------------------------------------- Two-Dimension + multiple rows and columns + always rectangular! no ragged arrays in MATLAB (unless you use something called a CELL ARRAY) + MATLAB is effectively row-major: rows: spaces/commas separate elements cols: semicolons separate rows >> [1 2; 3 4] % square matrix with rows [1 2] and [3 4] + rectangularity must be preserved >> [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? + different tricks and operations to try (1) Transpose: - swaps rows with columns - means that Aij becomes Aji for all combinations of i and j - use transpose operator $'$ >> x = [1 2 3]; >> x' >> A = [1 2; 3 4]; >> A' >> r1 = [1 3]; r2 = [2 4]; >> [r1' r2'] (2) Single indexing of multidimensional array - A(index) converts the entire array into one column array, where the elements are arranged column-by-column - very obscure syntax that few knowm besides DIS, who loves this kind of detail >> A=[1 2; 3 4]; >> A(3) % hint: MATLAB index starts from 1 and think columns.... ------------------------------------------------------------------------------- Builtin arrays: + see values.txt (see also zeros, ones, eye, rand, diag) + [] is the EMPTY ARRAY! >> x = [] + use colon (:) to create a 1D array automatically syntax: START:INCREMENT:STOP >> 1:3 >> 1:2.9 >> 3:-1:0 + may also use LINSPACE to create a vector - 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) ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Matrices + same structure and look as an array + usually means rectangular or square, but could have more dimensions + a matrix is an array that represents a linear transformation Ax = b A: coefficient matrix x: solution vector b: source vector example) 2x-2y = 10 \ [ 2 -2] {x} = {10} >-> [-2 5] {y} {20} -2x+5y = 20 / A x b So, matrix A transforms x into b To find x, you need to solve the set of simultaneous equations See CS100J-->MATLAB-->Math Links-->Solving Systems of Equations ------------------------------------------------------------------------------- Arithmetic operations: + see HELP ARITH, HELP OPS + addition: + and - work element by element: Aij+Bij=Cij + multiplication: * and / are a bit different! >> [1 2] + [3 4] % works for both matrices and arrays >> [1 2] .* [3 4] % array multiplication >> [1 2] * [3 4] % matrix multiplication ------------------------------------------------------------------------------- Functions: + MATLAB will generate an array of values regardless of input >> sqrt(4) >> sqrt([1 4 16]) ------------------------------------------------------------------------------- Indexing: + MATLAB INDEXS STARTS FROM 1, NOT 0!!!!! + finding the location (or, position) of an element in an array 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 ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- 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: + colon - besides generating an array, can help with indexing - see HELP COLON + all elements: >> A = [1 2; 3 4]; >> A(:) % get all elements of A in column major order >> A(:,:) % get all elements of A in "regular" order (redundant!) + subarrays (portion of the array): - use vector of indicies (a 1-D array of indicies) - 1D: name(vector) - 2D: name(vector,vector) 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: + 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 elements with locations (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 ------------------------------------------------------------------------------- Insertion: + think 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 + so, something like >> A=[1:3; 4:6]; >> A([1 2],:) = A([2 1],:) would be interpreted as the following: >> A gets the array [1 2 3] [4 5 6] >> the NEW first and second rows of A get the OLD second and first rows of A So, the output is..... + you can insert "beyond" the given dimensions! MATLAB fills in needed "spots" with zeros >> A=[1 2; 3 4]; >> A(6,6) = 10 ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Useful functions for arrays: + SIZE: >> A = [1:3; 4:6]; >> [row col] = size(A) + LENGTH: >> length(A) + END: >> A(2,1:end) ------------------------------------------------------------------------------- Logic: + logical values: 0 (false) and 1 (true) (actually, anything non-zero is true) + logical operators: & (and), | (or), ~ (not), xor (exclusive or) + relational operators: >, >=, <, <=, ==, ~= + examples) 1 < 2 % less than 1 == 1 % equal (true) 1 == 2 % equal (false) 1==1 & 2==2 % true 1 > 2 | 1 < 2 % true xor(1 < 2,1 < 3) % false 5 & 10 % true (anything non-zero is technically true!) ------------------------------------------------------------------------------- Logical Arrays + an array of logical values + operations are applied to each element one at a time + MATLAB stores 1 or 0 depending on the truth of the relation for each element >> x = [1 2 3]; >> y = x >= 2 + to extract elements that correspond to TRUE relations, use array(logicalarray) >> x(y) + Example 2: find even integers from an array >> z = 0:10; % array from 0 to 10 >> k = mod(z,2)==0; % $k$ is a logical array >> z(k) % extract even integers from $k$ + WARNING: to generate logical array, MUST use relations, not just enter 0 and 1! >> z=3:5; % array of ints from 3 to 5 >> q=[0 1 0]; % array of what appears to be logical values (but isn't!) >> z(q) % attempt to extract elements from $z$ (but won't work) output is the error mesg: ??? Index into matrix is negative or zero. See release notes on changes to logical indices. >> To fix, use $logical$ function: >> z(logical(q)) -------------------------------------------------------------------------------