CS 175 Project in AI Discussion -- matlab Yutian Chen
help help help help Documentation doc help
Useful functions who, whos size, length clear clc sum, max, min
Matrix Allocation Enter an explicit list of elements. Colon operator Generate matrices using built-in functions. linspace, logspace, zeros, ones, eye, rand Tips: Pre-allocation avoids reallocation of matrices
Matrix Indexing end, find exercise: Subscripted Indexing (1-based) A = [11 14 17; ... % use “…” to continue on the next line 12 15 18; ... 13 16 19]; A(2:3,2) Linear Indexing (matrix is store by column) A([3,1,8]) Logical Indexing A(logical([0 0 1 0 1])) end, find exercise: last column of A A(:,end) diagonal of A diag(A) or A(logical(eye(size(A)))) Find all the elements greater than 15 A(A>15)
Matrix Manipulation Transpose A=A' A(:) repmat reshape [] Cat
Matrix Manipulation cont. exercise: A=ones(2,2); B=rand(2); concatenate A and B along the 1st, 2nd, 3rd dimension 1st: [A;B] or cat(1,A,B) 2nd: [A,B] or cat(2,A,B) 3rd: cat(3,A,B) generate [1 1 1 1 1 2 2 2 2 2] [ones(1,5),2*ones(1,5)] or reshape(repmat([1, 2],5,1),1,10) transform A=[1 2; 3 4; 5 6; 7 8] to [1 2 3 4; 5 6 7 8] A = reshape(A',4,2)'
Flow Control if, else, switch for loop while loop continue break for i = [1 3 5 6]; H(i) = 1/i; end while loop continue break
Logical operators Element-wise operator &, |, ~ <, <=, >, >=, ==, ~= A=ones(2,2); B=A; A==B Some functions to reduce the results of matrix comparisons to scalar conditions: isequal, isempty, all, any
Save/Load save load save a.mat save a.mat x y save('a.mat','x','y') load a.mat load a.mat x load('a.mat','x')
Vectorization For simplicity and speed Vector dot product A=[1 2 3 4 5]; B=[5 4 3 2 1]; p = 0; for i=1:5 p=p+A(i)*B(i); end Or p = A*B'; p = sum(A.*B);
Vectorization cont. Matrix multiplication A=ones(5); B=eye(5); p = zeros(size(A,1),size(B,2)); for i=1:size(p,1) for j=1:size(p,2) for k=1:size(p,2) p=p+A(i,k)*B(k,j); end Or p = A*B Compare the multiplication of two 100x100 matrix to see the difference
Vectorization cont. Exercise A is a row vector, C=A(1)*A(2)+A(2)*A(3)+...+A(N- 1)*A(N); C = A(1:end-1)*A(2:end) ' polynomial evaluation: a0+a1*x+...+ak*x^k y = A*2.^(0:k) '; sigmoid(x,W,b) (x: dims:1xD, W: 1xD, b:1x1) 1/(1+exp( -(W*x‘+b) )) sigmoid(x,W,b) (x: NxD, W: 1xD, b:1x1) 1./(1+exp( -(W*x‘+b) ))
Vectorization cont. Exercise weight update in logistic regression y is a Nx1 row vector of labels x is a NxD matrix of data W is a 1XD row vector W = W + rate * x' *(y-1./(1+exp( -(W*x' +b) ))) (Be careful to match the inner dimension of matrix multiplications)
Plot plot line type, color, symbol plot(x,y); plot(y); plot(A) line type, color, symbol title, xlabel, legend, xlim, hold on/off log-scale: semilogx, semilogy, loglog hist, bar, stem, surf, scatter
Script/Function Script Function Debugging a batch of commands grouped in a .m file share the same workspace with the command line Function keyword: function input, output function name determined by the file name useful functions: nargin, nargout, exist Debugging