% what does matlab do when you want it to treat a matrix as a vector? >> a = sin(rand(3,4)) a = 0.7967 0.3947 0.3989 0.3456 0.6730 0.8049 0.7794 0.7265 0.1754 0.7937 0.0579 0.0099 >> a(4) ans = 0.3947 % Apparently it treats is as the column vector made up from its columns, from % first to last. % In fact, a(:) is explicitly that equivalent column vector, as you can see: >> a(:) = 1:12 a = 1 4 7 10 2 5 8 11 3 6 9 12 % question from the audience: suppose you would rather have the entries of % 1:12 put in row by row, how would you do it? % Several possibilities: >> b = zeros(4,3); b(:) =1:12; >> b' ans = 1 2 3 4 5 6 7 8 9 10 11 12 % perhaps better: >> reshape(a,4,3)' ans = 1 2 3 4 5 6 7 8 9 10 11 12 % matlab commands come in (at least) three flavors: those implementing % functions of scalars, like sin, exp % functions of vectors, like sum, prod, max, min, cumsum, cumprod, sort, ... % functions of matrices, like size, ... % What happens if their argument is not of the `right' type? % matlab etiquette: a function of a scalar should work on a matrix by applying % the function entrywise. >> sin(a) ans = 0.8415 -0.7568 0.6570 -0.5440 0.9093 -0.9589 0.9894 -1.0000 0.1411 -0.2794 0.4121 -0.5366 % matlab etiquette: a function of a vector works on a matrix by applying the % function to column-wise: >> sum(a) ans = 6 15 24 33 % What if you would rather have it row-wise? % Then you can say so by specifying an additional input argument as 2: >> a a = 1 4 7 10 2 5 8 11 3 6 9 12 >> sum(a,2) ans = 22 26 30 >> max(a) ans = 3 6 9 12 >> max(a,2) ans = 2 4 7 10 2 5 8 11 3 6 9 12 % Well, that's unexpected! There is an additional wrinkle with max and min: % max(A,B), if legal, will return the matrix whose (i,j)-entry is the % max of A(i,j) and B(i,j). % Also, remember matlab's wrinkle concerning scalars: If matlab expects a % matrix of a certain size and is, instead, given a scalar, it will replace % that scalar by the matrix of the expected size having all its entries % equal to the scalar. E.g., >> b = a+1 b = 2 5 8 11 3 6 9 12 4 7 10 13 % So, the statement max(a,2) compared each entry of a with 2 and chose the % bigger of the two. % % To get the max for each row, you have to specify that optional second % argument as empty, [], and put in the 2 as the optional third argument: >> max(a,[],2) ans = 10 11 12 % be aware of cumsum, and cumprod: >> cumsum(1:4) ans = 1 3 6 10 >> cumprod(1:4) ans = 1 2 6 24 % the rest of the class was devoted to polynomial interpolation, starting off % with matlab's polyfit command: % To get the polynomial interpolant to given data x,y, use % d = length(x)-; p = polyfit(x,y,d) % What if you use larger d??? >> x = 1:3; y = x.^4; >> p = polyfit(x,y,4) p = 1.0000 0.0000 0 0 -0.0000 >> p = polyfit(x,y,5) p = 0.0000 1.0000 0 0 0 0.0000 >> p = polyfit(x,y,7) p = Columns 1 through 7 -0.1299 0.5000 0 0 0 0 0 Column 8 0.6299 >> p = polyfit(x,y,10) p = Columns 1 through 7 -0.0253 0.0801 0 0 0 0 0 Columns 8 through 11 0 0 0 0.9453 >> xx = linspace(1,3); >> plot(xx,polyval(p,xx),x,y,'o') % ... so, this polynomial does interpolate to the data. Let's compare it % with the standard one: >> ps = polyfit(x,y,length(x)-1) ps = 25.0000 -60.0000 36.0000 >> hold on >> plot(xx,polyval(ps,xx),'r','linew',2) >> hold off % It seems that polyfit constructs the unique interpolating polynomial % of the form % a(1)x^(d-1) + a(2) x^(d-2) + ... + a(n-1) x^(d+1-n) + a(n) % I would have preferred the polynomial of lowest possible degree, i.e., % of the form % a(1)x^(n-1) + a(2) x^(n-2) + ... + a(n-1) x^(1) + a(n) % The rest of the class time was spent on discussing the Newton form of the % interpolating polynomial and the construction of divided differences, etc. diary off