%% Programming Languages Matlab 3101 - Class 3 % 3/26/08, Instructor: Blake Shaw %% Notes: HW1 grades, Quiz, % HW 1, avg is 82.5, std deviation is 14, quiz is next week, it will be the % second half of class, 1 hour, closed book, closed computer, closed % everything. %% Review % Loops, and Control flow: if, while, for, try/catch. Displaying text with % formatting. More plotting: spy, imagesc. %% Today: focus on data structures % matrices (including numbers, and lists), strings, cells, saving and % loading %% Numbers, Lists, and Matrices % show some examples with [], cat, indexing, transpose, flip, rot90, % reshape, flattening with (:) %% a = 34; A = [4, 5; 6, 8; 10, 15] B = [7, 8; 10, 12] C = [A; B; [9, 8]] C = [A', B] D = [A(1:2, 1:2), B] %% A = round(10*rand(3, 2)) B = round(10*rand(3, 2)) C = round(10*rand(3, 2)) D = cat(3, A, B, C); size(D) %% A = round(20*rand(3, 3)); fliplr(A) flipud(A) A rot90(A) sum(sum(sum(D))) sum(D(:)) flatD = D(:); reshape(flatD, 3, 6) %% Creating special matrices % zeroes, ones, eye, diag, linspace, :, rand, randn A = zeros(3, 2, 4) A = ones(3, 3) A = eye(3, 3) A = diag(eye(3, 3)) A = diag([2, 3, 5, 7]) b = linspace(-pi, pi, 1000); length(b) plot(b, sin(b)) a = 6:0.2:9; length(a) %% a = rand(10000, 1); b = randn(10000, 1); hist(a) hist(b) %% Matrices can be high-dimensional % squeeze, shiftdim D = floor(10*rand(3, 2, 3)) D2 = sum(D, 2) size(D2) D3 = squeeze(D2) size(D3) %% D size(D) D4 = shiftdim(D, 3) size(D4) %% circshift A = floor(10*rand(5, 5)) %% A circshift(A, 1) circshift(A, [1, 1]) %% Matrices can be sparse % sparse, nnz, nonzeros, sprand N = 10^3; A = zeros(N, N); size(A) %% N = 10^5; B = sparse(N, N); size(B) %% B(1, 5) = 9; B(4, 9) = 12; B = B .^ 2; B(1, 5) %% nnz(B) nonzeros(B) C = sprand(N, N, 0.0005); %% nnz(C) imagesc(C(1:100, 1:100)); axis image; %% Creating indexes % :, randperm, sub2ind, ind2sub X = rand(100,1); N = length(X); i = randperm(N); X(i(1:10)) [i, j] = ind2sub([5, 5], 14) %% Avoiding loops % repmat, find, circshift, relational operators, sort, permute a = rand(10, 10); %% [i, j] = find(a < 0.5) [i, j] %% a = [1, 2, 3] a([2, 1, 3]) %% X = rand(100, 2); p = rand(1, 2); %% figure(1); clf; hold on; scatter(X(:, 1), X(:, 2)) scatter(p(1, 1), p(1, 2), 'r+') hold off; %% dists = sum((X - repmat(p, 100, 1)).^2, 2); i = find(dists < 0.01) [C, I] = min(dists) %% Strings % declaring, concatenating, indexing, converting, strfind, strtok a = 'hello'; length(a) b = [a, ' blake']; strfind(b, 'll') strfind(b, 'afdas') %c = ['string1'; 'this is string 2'; 'hi'] %% basic types: double, char unit8 char(double(b) + 1) b(5) b(2:5) %% Cells % declaring, concatenating A = cell(4, 1); A{1} = 'this is a string'; A{2} = rand(10, 10); A{3} = 'this is another string'; A{4} = cell(2, 2); B = cell(2, 2) C = cell(2, 2) D = {B, C} D = {rand(10), 10, 'hello'} D = [B C] %% Saving and Loading variables % who, whos, clear, save, load whos save results.mat clear whos whos -file results.mat load results.mat %% User Input % input, str2num s = input('what is the temp today?\n', 's'); %% str2num(s) + 1 %% About HW3 % Talk about rot13 encoding, and the caesar cipher %% Quiz review %{ Class Notes 1:3 Working with matrices: http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/math/f4-988203.html Basic Flow Control: http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/learn_matlab/f4-1931.html %}