|
| 1 | +% octave cheatsheet |
| 2 | +% not equals(~=) |
| 3 | +1 ~=2 % => 1 |
| 4 | + |
| 5 | +% change prompt |
| 6 | +PS1('>> '); |
| 7 | + |
| 8 | +% semicolon suppresses output |
| 9 | + |
| 10 | + |
| 11 | +% print |
| 12 | +disp( 1+1) |
| 13 | +disp(sprintf('2 decimals: %0.2f', 3.146)) |
| 14 | +format long |
| 15 | +format short |
| 16 | + |
| 17 | + |
| 18 | +% matrix |
| 19 | +A = [1 2; 3 4 ; 5 6] |
| 20 | +A = |
| 21 | + |
| 22 | + 1 2 |
| 23 | + 3 4 |
| 24 | + 5 6 |
| 25 | + |
| 26 | +% vector |
| 27 | +v = [1; 2; 3] |
| 28 | + |
| 29 | +% range fat vector |
| 30 | +v = 1: 0.1: 1.5 |
| 31 | +v = |
| 32 | + |
| 33 | + 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 |
| 34 | + |
| 35 | +% matrix of ones |
| 36 | +ones(2,3) |
| 37 | +ans = |
| 38 | + |
| 39 | + 1 1 1 |
| 40 | + 1 1 1 |
| 41 | + |
| 42 | + |
| 43 | +zeroes(1,3) % matrix of zeroes |
| 44 | +rand(3,3) % matrix of random numbers |
| 45 | +eye(3) # identity matrix |
| 46 | +ans = |
| 47 | + |
| 48 | +Diagonal Matrix |
| 49 | + |
| 50 | + 1 0 0 |
| 51 | + 0 1 0 |
| 52 | + 0 0 1 |
| 53 | + |
| 54 | +v = [1,2,3,4] |
| 55 | +% length returns longest dimension |
| 56 | +length(v) % => 4 |
| 57 | + |
| 58 | +% size returns matrix of size |
| 59 | +size(v) % => [1,4] |
| 60 | + |
| 61 | + |
| 62 | +% working with files |
| 63 | + load file |
| 64 | + load featuresX.dat |
| 65 | + |
| 66 | + who % shows variables in current scope |
| 67 | + whos % shows variables and size |
| 68 | + |
| 69 | + clear featuresX % removes variable featuresX |
| 70 | + |
| 71 | + % save to disk |
| 72 | + v = [1;2;3;4;5] |
| 73 | + |
| 74 | + save hello.mat v; % saves v to file hello.mat |
| 75 | + |
| 76 | + clear % removes all variables in workspace |
| 77 | + |
| 78 | + save hello.mat v -ascii; % save as text |
| 79 | + |
| 80 | +A(3,2) % gets the value on row 3, column 2 |
| 81 | +A(2,:) % get every element in row 2 |
| 82 | + |
| 83 | +A = [A, [100; 101; 102]] % append another column to a |
| 84 | + |
| 85 | +% ============================== |
| 86 | + |
| 87 | +A = [1 2 ; 3 4 ; 5 6] |
| 88 | +B = [11 12; 13 14; 15 16] |
| 89 | +C = [1 1; 2 2] |
| 90 | + |
| 91 | +% element wise multiplication (.*) |
| 92 | + |
| 93 | +A .* B |
| 94 | +ans = |
| 95 | + |
| 96 | + 11 24 |
| 97 | + 39 56 |
| 98 | + 75 96 |
| 99 | + |
| 100 | +abs(A) % absolute value |
| 101 | + |
| 102 | +% transpose |
| 103 | +A' |
| 104 | +ans = |
| 105 | + |
| 106 | + 1 3 5 |
| 107 | + 2 4 6 |
| 108 | + |
| 109 | +A = magic(3) % magic squares, all rows columns and diagonals add up to the same thing |
| 110 | + |
| 111 | +[r,c] = find(A >= 7) |
| 112 | +r = |
| 113 | + 1 |
| 114 | + 3 |
| 115 | + 2 |
| 116 | + |
| 117 | +c = |
| 118 | + 1 |
| 119 | + 2 |
| 120 | + 3 |
| 121 | + |
| 122 | + |
| 123 | +sum(A, 1) % sums rows |
| 124 | +sum(A, 2) % sums columns |
| 125 | +prod |
| 126 | +floor |
| 127 | +ceil |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +flipud(A) % flips matrix up down |
| 132 | + |
| 133 | +pinv(A) % gives the inverse of A |
| 134 | + |
| 135 | + |
| 136 | +% plotting |
| 137 | +t = [0:0.01: 0.98]; |
| 138 | +y1 = sin(2*pi*4*t); |
| 139 | + |
| 140 | +plot(t, y1); |
| 141 | + |
| 142 | + |
| 143 | +y2 = cos(2*pi*4*t) |
| 144 | +plot(t,y1); |
| 145 | +hold on |
| 146 | +plot(t,y2); |
| 147 | +plot(t,y2, 'r'); |
| 148 | +xlabel('time') |
| 149 | +ylabel('value') |
| 150 | +legend('sin', 'cos') |
| 151 | +title('my plot') |
| 152 | + |
| 153 | +print -dpng 'myplot.png' |
| 154 | +close |
| 155 | + |
| 156 | +figure(1); plot(t,y1); |
| 157 | +figure(2); plot(t,y2); |
| 158 | +subplot(1,2,1); %divides plot a 1x2 grid access first element |
| 159 | +plot(t, y1); |
| 160 | +subplot(1,2,2) |
| 161 | +plot(t, y2) |
| 162 | + |
| 163 | +clf % clears figure |
| 164 | + |
| 165 | + |
| 166 | +imagesc(A), colorbar, colormap gray; |
| 167 | + |
| 168 | + |
| 169 | +% ================================= |
| 170 | + |
| 171 | +v = zeros(10,1) |
| 172 | + |
| 173 | +for i = 1:10, |
| 174 | + v(i) = 2^i; |
| 175 | +end |
| 176 | + |
| 177 | + |
| 178 | +indicies = 1:10 |
| 179 | +for i = indices, |
| 180 | + disp(i); |
| 181 | +end; |
| 182 | + |
| 183 | +i = 1; |
| 184 | +while i <= 5, |
| 185 | + v(i) = 100; |
| 186 | + i = i+1; |
| 187 | +end; |
| 188 | + |
| 189 | +i = 1; |
| 190 | +while true, |
| 191 | + v(i) = 999; |
| 192 | + i = i + 1; |
| 193 | + if i == 6, |
| 194 | + break; |
| 195 | + end; |
| 196 | +end; |
| 197 | + |
| 198 | + |
| 199 | +v(1) = 2 |
| 200 | +if v(1) = 2; |
| 201 | + disp('the value is one'); |
| 202 | +elseif v(1) == 2, |
| 203 | + disp('the value is true'); |
| 204 | +else |
| 205 | + disp("the value is not one or two."); |
| 206 | +end; |
| 207 | + |
| 208 | + |
| 209 | +squareThisNumber.m |
| 210 | +function y = squareThisNmber(x) |
| 211 | +y = x^2; |
| 212 | + |
| 213 | +suareAndCuebeThisNumber.m |
| 214 | +function [y1,y2] = squareAndCubeThisNumber(x) |
| 215 | +y1 = x^2; |
| 216 | +y2 = x^3; |
| 217 | + |
| 218 | + |
| 219 | +X = [1 1; 1 2; 1 3] |
| 220 | +y = [1; 2; 3] |
| 221 | + |
| 222 | +theta = [0;1]; |
| 223 | + |
| 224 | +costFunctionJ.m |
| 225 | + function J = costFunctionJ(X, y, theta) |
| 226 | + |
| 227 | + % X is the 'design matrix' containing our training examples |
| 228 | + % y is the class labels |
| 229 | + |
| 230 | + m = size(X, 1) % number of training examples |
| 231 | + predictions = X*theta; % predictions of hypothesis on examples |
| 232 | + sqrErrors = (predictions - y).^2; % squared errors |
| 233 | + |
| 234 | + J = 1/(2*m) * sum(sqrErrors); |
| 235 | + |
| 236 | + |
| 237 | +J % => 0 |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | +Theta1 = ones(10,11); |
| 242 | +Theta2 = ones(10,11); |
| 243 | +Theta3 = 3*ones(10,11); |
| 244 | + |
| 245 | +thetaVec = [ Theta1(:); Theta2(:); Theta3(:)]; |
| 246 | + |
| 247 | +Theta1 == reshape(thetaVec(1:110), 10, 11) |
| 248 | + |
| 249 | +gradApprox = (J(theta = EPSILON) - J(theta - EPSILON))/(2*EPSILON) |
| 250 | + |
| 251 | +for i = 1:n, |
| 252 | + thetaPlus = theta; |
| 253 | + thetaPlus(i) = thetaPlus(i) + EPSILON; |
| 254 | + thetaMinus = theta; |
| 255 | + thetaMinus(i) = thetaMinus(i) - EPSILON; |
| 256 | + gradApprox(i) = (J(thetaPlus) - J(thetaMinus))/(2*EPSILON); |
| 257 | +end |
| 258 | + |
| 259 | +% check that gradApprox ~ DVec |
| 260 | + |
| 261 | +% - implement backprom to compute DVec (unrolled D1, D2, D3) |
| 262 | +% - implement numerical gradient check to compute gradApprox |
| 263 | +% - make sure they give similar values |
| 264 | +% - TURN OFF gradient checking using backprob code for learning with |
| 265 | +% NO gradient checking |
| 266 | + |
| 267 | +optTheta = fminunc(@costFunction, initialTheta, options); |
| 268 | + |
| 269 | +initialTheta = zeros(n,1); % can we do better? |
| 270 | + |
| 271 | +% INIT_EPSILON is unrelated to EPSILON |
| 272 | +Theta1 = rand(10,11) * (2*INIT_EPSILON) - INIT_EPSILON; |
| 273 | +Theta1 = rand(1,11) * (2*INIT_EPSILON) - INIT_EPSILON; |
| 274 | + |
| 275 | +load('ex3data1.mat'); |
0 commit comments