5.5 control statement: For, while, if statement
Reference video: 5-5-control statements _ for, while, if statements (13 min 2.16.mkv
1. For Loop access column vector through Index
1 >> v = zeros(10,1) 2 v = 3 0 4 0 5 0 6 0 7 0 8 0 9 010 011 012 013 >> for i = 1 : 10,14 v(i) = 2 ^ i;15 end;16 >> v17 v =18 219 420 821 1622 3223 6424 12825 25626 51227 1024
2. For loop direct access to column vector Elements
1 >> indices = 1 : 10; 2 >> indices 3 indices = 4 1 2 3 4 5 6 7 8 9 10 5 >> for i = indices, 6 disp(i); 7 end; 8 1 9 210 311 412 513 614 715 816 917 10
3. While loop access column vector
1 >> i = 1; 2 >> while i <= 5, 3 v(i) = 100; 4 i = i + 1; 5 end; 6 >> v 7 v = 8 100 9 10010 10011 10012 10013 6414 12815 25616 51217 1024
4. While (true) and break
1 >> i = 1 ; 2 >> while(true), 3 v(i) = 999; 4 i = i + 1; 5 if i == 6, 6 break; 7 end; 8 end; 9 >>10 >> v11 v =12 99913 99914 99915 99916 99917 6418 12819 25620 51221 1024
5. If else statement
1 >> v(1) = 2;2 >> if v(1) == 1,3 disp(‘The value is one‘);4 elseif v(1) == 2,5 disp(‘The value is two‘);6 else,7 disp(‘The value is not one or two‘);8 end;9 The value is two
6. User-Defined Function
The squarethisnumber (x) function is defined as follows:
1 function y = squareThisNumber(x)2 y = x^2;3 endfunction
Save the function as squarethisnumber. M. Note that it is in lower case.
At this time, the squarethisnumber (2) of the called function prompts that the function cannot be found.
1 >> squareThisNumber(2);2 error: ‘squareThisNumber‘ undefined near line 1 column 13 >>4 >> ls5 [.] featuresX.dat priceY.dat6 [..] hello.dat squarethisnumber.m
Name the file squarethisnumber. m (case-insensitive), and the function is called successfully.
1 >> ls2 [.] featuresX.dat priceY.dat3 [..] hello.dat squareThisNumber.m4 >> squareThisNumber(2);5 >> a = squareThisNumber(2);6 >> a7 a = 4
This indicates that Ave ave is case sensitive and the file name must be the same as the function name.
7. Functions with multiple return values
If the Ave ave function is different from other languages, multiple values can be returned. The definition method squareandcubethisnumber (X) is as follows:
1 function [y1, y2] = squareAndCubeThisNumber(x),2 y1 = x ^ 2;3 y2 = x ^ 3;4 endfunction
Call:
1> A = squareandcubethisnumber (2) % accept the first return value 2 A = 43> [a, B] = squareandcubethisnumber (2) % accept two return values 4 A = 45 B = 8
8. More complex functions
Save the following function to costfunctionj. M.
1 Function J = costfunctionj (X, Y, theta), 2% X is the "design matrix" containing our training examples 3% Y is the class labels 4 5 m = size (X, 1); % Number of training examples, size of rows 6 predictions = x * Theta; % predictions of hapothesis on all M Examples 7 sqrerrors = (predictions-Y ). ^ 2; % squared errors. ^ indicates the square of each element in the data 8 9 j = 1/(2 * m) * sum (sqrerrors); 10 11 endfunction
Initializes the matrix for the preceding dataset. Call a function to calculate the value of the cost function.
1> X = [1 1; 1 2; 1 3]; 2> Y = [1; 2; 3]; 3> Theta = [0; 1]; % records is 0, 1 h (x) = x. The value of the cost function is 04> J = costfunctionj (X, Y, theta) 5 J = 0.
1> Theta = [0; 0]; % values is 0, 0 h (x) = 0. data cannot be fitted at this time. 2> J = costfunctionj (X, Y, theta) 3 J = 2.33334 5> (1 ^ 2 + 2 ^ 2 + 3 ^ 2)/(2*3) % value of the cost function 6 ans = 2.3333
[Original] Andrew Ng Stanford Machine Learning (5) -- lecture 5 Ave ave tutorial-5.5 control statement: For, while, if statement