Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 175 Project in AI Discussion -- matlab

Similar presentations


Presentation on theme: "CS 175 Project in AI Discussion -- matlab"— Presentation transcript:

1 CS 175 Project in AI Discussion -- matlab
Yutian Chen

2 help help help help Documentation doc help

3 Useful functions who, whos size, length clear clc sum, max, min

4 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

5 Matrix Indexing end, find exercise: Subscripted Indexing (1-based)
A = [ ; % use “…” to continue on the next line ; ... ]; A(2:3,2) Linear Indexing (matrix is store by column) A([3,1,8]) Logical Indexing A(logical([ ])) 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 A(A>15)

6 Matrix Manipulation Transpose A=A' A(:) repmat reshape [] Cat

7 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 [ ] [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 [ ; ] A = reshape(A',4,2)'

8 Flow Control if, else, switch for loop while loop continue break
for i = [ ]; H(i) = 1/i; end while loop continue break

9 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

10 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')

11 Vectorization For simplicity and speed Vector dot product
A=[ ]; B=[ ]; p = 0; for i=1:5 p=p+A(i)*B(i); end Or p = A*B'; p = sum(A.*B);

12 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

13 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) ))

14 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)

15 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

16 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


Download ppt "CS 175 Project in AI Discussion -- matlab"

Similar presentations


Ads by Google