-
Notifications
You must be signed in to change notification settings - Fork 256
/
octave_session.m
254 lines (197 loc) · 4.97 KB
/
octave_session.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
% Machine learning class
% Octave tutorial
% =======================================================
% Section 1: Octave Tutorial: Basic operations
%% Change Octave prompt
PS1('>> ');
%% elementary operations
5+6
3-2
5*8
1/2
2^6
1 == 2 % false
1 ~= 2 % true. note, not "!="
1 && 0
1 || 0
xor(1,0)
%% variable assignment
a = 3; % semicolon suppresses output
b = 'hi';
c = 3>=1;
% Displaying them:
a = pi
disp(sprintf('2 decimals: %0.2f', a))
disp(sprintf('6 decimals: %0.6f', a))
format long
a
format short
a
%% vectors and matrices
A = [1 2; 3 4; 5 6]
v = [1 2 3]
v = [1; 2; 3]
v = [1:0.1:2] % from 1 to 2, with stepsize of 0.1. Useful for plot axes
v = 1:6 % from 1 to 6, assumes stepsize of 1
C = 2*ones(2,3) % same as C = [2 2 2; 2 2 2]
w = ones(1,3) % 1x3 vector of ones
w = zeros(1,3)
w = rand(1,3) % drawn from a uniform distribution
w = randn(1,3) % drawn from a normal distribution (mean=0, var=1)
w = -6 + sqrt(10)*(randn(1,10000)) % (mean = 1, var = 2)
hist(w)
I = eye(4) % 4x4 identity matrix
% help function
help eye
help rand
% =======================================================
% Section 2: Octave Tutorial: Moving data around
%% dimensions
sz = size(A)
size(A,1) % number of rows
size(A,2) % number of cols
length(v) % size of longest dimension
%% loading data
pwd % show current directory (current path)
cd 'C:\Users\ang\Octave files' % change directory
ls % list files in current directory
load q1y.dat
load q1x.dat
who % list variables in workspace
whos % list variables in workspace (detailed view)
clear q1y % clear w/ no argt clears all
v = q1x(1:10);
save hello v; % save variable v into file hello.mat
save hello.txt v -ascii; % save as ascii
% fopen, fread, fprintf, fscanf also work [[not needed in class]]
%% indexing
A(3,2) % indexing is (row,col)
A(2,:) % get the 2nd row.
% ":" means every element along that dimension
A(:,2) % get the 2nd col
A([1 3],:)
A(:,2) = [10; 11; 12] % change second column
A = [A, [100; 101; 102]]; % append column vec
A(:) % Select all elements as a column vector.
% Putting data together
A = [A [100; 101; 102]]
B = [11 12; 13 14; 15 16] % same dims as A
[A B]
[A; B]
% =======================================================
% Section 3: Octave Tutorial: Computing on data
%% matrix operations
A * C % matrix multiplication
A .* B % element-wise multiplcation
% A .* C or A * B gives error - wrong dimensions
A .^ 2
1./v
log(v) % functions like this operate element-wise on vecs or matrices
exp(v) % e^4
abs(v)
-v % -1*v
v + ones(1,length(v))
% v + 1 % same
A' % matrix transpose
%% misc useful functions
% max (or min)
a = [1 15 2 0.5]
val = max(a)
[val,ind] = max(a)
% find
a < 3
find(a < 3)
A = magic(3)
[r,c] = find(A>=7)
% sum, prod
sum(a)
prod(a)
floor(a) % or ceil(a)
max(rand(3),rand(3))
max(A,[],1)
min(A,[],2)
A = magic(9)
sum(A,1)
sum(A,2)
sum(sum( A .* eye(9) ))
sum(sum( A .* flipud(eye(9)) ))
% Matrix inverse (pseudo-inverse)
pinv(A) % inv(A'*A)*A'
% =======================================================
% Section 4: Octave Tutorial: Plotting
%% plotting
t = [0:0.01:0.98];
y1 = sin(2*pi*4*t);
plot(t,y1);
y2 = cos(2*pi*4*t);
hold on; % "hold off" to turn off
plot(t,y2,'r');
xlabel('time');
ylabel('value');
legend('sin','cos');
title('my plot');
print -dpng 'myPlot.png'
close; % or, "close all" to close all figs
figure(2), clf; % can specify the figure number
subplot(1,2,1); % Divide plot into 1x2 grid, access 1st element
plot(t,y1);
subplot(1,2,2); % Divide plot into 1x2 grid, access 2nd element
plot(t,y2);
axis([0.5 1 -1 1]); % change axis scale
%% display a matrix (or image)
figure;
imagesc(magic(15)), colorbar, colormap gray;
% comma-chaining function calls.
a=1,b=2,c=3
a=1;b=2;c=3;
% =======================================================
% Section 5: Octave Tutorial: For, while, if statements, and functions.
v = zeros(10,1);
for i=1:10,
v(i) = 2^i;
end
% Can also use "break" and "continue" inside for and while loops to control execution.
i = 1;
while i <= 5,
v(i) = 100;
i = i+1;
end
i = 1;
while true,
v(i) = 999;
i = i+1;
if i == 6,
break;
end;
end
if v(1)==1,
disp('The value is one!');
elseif v(1)==2,
disp('The value is two!');
else
disp('The value is not one or two!');
end
% exit % quit
% Functions
% Create a file called squareThisNumber.m with the following contents (without the %):
% function r = squareThisNumber(x)
% r = x * x;
% end
squareThisNumber(5);
% If function is undefine, use "pwd" to check current directory (path),
% and "cd" to change directories
pwd
cd 'C:\Users\ang\Desktop';
squareThisNumber(5);
% Octave search path (advanced/optional)
addpath('C:\Users\ang\Desktop');
cd 'C:\'
squareThisNumber(5);
% If you have defined other functions such as costFunctionJ,
% the following code will work too.
X = [1 1; 1 2; 1 3];
y = [1;2;3];
theta = [0; 1];
j = costFunctionJ(X, y, theta);
theta = [0; 0];
j = costFunctionJ(X, y, theta);