lecture thursday, 4/20 + turn in exercise 7 in the provided piles [a-c, d-i, j-l, m-r, s-z] for tuesday: + lecture is on inheritance: please review ahead of time pre-inheritance box/scope concepts + exercise 8 % assign to $table1$ the row vector of column sums of table2 % assign to $table5$ the column vector of row sums of table3 % assign to $dist$ the distance between table3 and table4 % modify table2: swap columns 2 and 5 of table2 some matlab basics % online help -- try $help$ on : $help$, $lookfor$, $who$, $type$ help help % *scripts* (.m files) -- can type code into a file and run it % (we will see functions later) help echo help run help script % character, string: array of characters (characater = 1-by-1 array) 'start the attack' 'start the attack' + 1 char('start the attack' + 1) % <-- messes up spaces % scalars -- same as in java % use $,$ to combine multiple statements with printed answers % use $;$ to combine multiple statements with suppressed output 2, -3.14, 4e2 5; 6.28; 7e3; _______________________________________________________________________________ creating new arrays in matlab % arrays: put entries inside brackets $[$, $]$ % horizontal concatenation: juxtaposition (side by side with spaces or commas) % vertical concatenation: go to next line or use $;$ (grr! dual use of $;$) [ 2 3 4 ] [ 2, 5, 8 10] % <-- not a typo [ 1 8 -7 ] [ 2 ; 8 ; 70 ] [ 1 2 3 4 5 6 7 8 9 ] [ 10 11 12 ; 31 32 33 ; 48 4 0 ] [[ 1 ; 2 ; 3 ] [ 4 ; 5 ; 6 ] [ 7 ; 8 ; 9] ] % note on concatenation: dimensions must match up [ 1 ; 2 3 ] [ 1 [ 2 ; 3 ] ] % evenly spaced intervals lo:hi, lo:step:hi, hi:-step:lo % note: will *not* go past second bound 1:10.3 % <-- does not go past 10.3 1:2:10 % <-- does not go past 10 10:1 % <-- empty! 1:-1:10 % <-- empty! 10:-1:1 10:-2:1 % <-- does not go past 1 % rand, ones, zeros: with no size given, return 1 number % with one size give, return square matrix % with two sizes given, return rectangular matrix ones rand(5) zeros(2,7) _______________________________________________________________________________ matlab array operations % scalar op scalar, f(scalar): as you expect 2 + 7, 2 - 7, 2 * 7, 2 / 7, 2 ^ 7, abs(-7), sqrt(49) % equal-sized arrays: $+$ and $-$ work element-wise % you may ignore code below for now, but look at output a = [1 2 3 4 5], b = [8 5 4 6 9], a+b fprintf('%4d+%d', [a ; b]), fprintf('\n') % now look at code again and make sure you understand output a = [ 1 2 3 ; 4 5 6 ], b = [ 3 1 4 ; 2 1 7], a - b % $'$ is transposition a, a' % scalar with array: $+$, $-$ work element-wise 3 - (1:10), (1:10) - 3 % use .^, .*, ./ for element-wise operations with one or two arrays % this is because $^$, $*$, $/$ do matrix (linear-algebra) operations % you are not expected to know linear algebra for cs100, % but you are welcome to use those operations if you know them. (1:5) .^ 2 2 .^ (1:5) % note: bad style to use .^, .*, or ./ with two scalars % $sum$, $max$, $prod$ on vectors: as we saw a = [4 1 5 2 8 3], sum(a), max(a), prod(a) % $sum$, $max$, $prod$ on 2-d arrays: operate on each *column* a = [ 4 1 5 ; 2 8 3], sum(a), max(a), prod(a) % $cumsum$, $cumprod$: cumulative sums or prodcuts cumsum([1 1 1 1]) cumsum([1 3 5]) % note: can find max value and also *where*: a = [ 4 1 5 2 8 3]; [ 1:length(a) ; a ] [maxv, where] = max(a) % <-- two results are returned! a(where) % <, >, <=, >=, ==, ~=: element-wise, with true=1, false=0 3 < (1:5) _______________________________________________________________________________ matlab array access and modification % since matlab uses $[$, $]$ to create arrays % it uses $($, $)$ to index into arrays % note: matlab indices start at 1, not 0 (grr!) % note: matlab uses row-major indexing % ignore for now -- grr! the elements are stored column-major order!! a = [ 3 1 4 ; 1 5 9 ; 2 6 5 ; 3 5 0 ] a(1,1), a(1,2), a(1,3) a(2,1), a(2,2), a(2,3) % remember, arrays are values -- no references, no aliases! b = a b(1,1) = -b(1,1) a % <-- unchanged! % indexing above is special case of more general indexing: % can access elements in multiples rows and columns -- a([1 2 3], [1 3]) % rows 1 2 3 (omit 4), columns 1 (omit 2) 3 a([1 2 3], [1 3]) = [ 7 7 ; 7 7 ; 7 7 ] % shorthand: $:$ by itself means "all of them" a(1, :) % row 1, all of the columns a(:, 2) % all of the rows, column 2 a(4:-1:1, [1 3]) % rows 4 3 2 1 (reverse order!), columns 1 and 3 a(ones(1,5), :) % row 1 five times, all of the columns