Note for Operations in Octave

Course notes on Octave operations.

Operations in Octave

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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
A = [1 2; 3 4; 5 6;]
A =

1 2
3 4
5 6

>> size(A) #returns a 1 by 2 matrix recording the size of matrix A
ans =

3 2

>> size(A, 1) #ROW
ans = 3
>> size(A, 2) #COL
ans = 2
>> v = [1 2 3 4]
v =

1 2 3 4

>> length(v) #length of a vector
ans = 4

>> who
Variables in the current scope:

A ans v

>> whos
Variables in the current scope:

Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
ans 1x19 19 char
v 1x4 32 double

Total is 29 elements using 99 bytes
>> clear v #to get rid of a variable
>> whos
Variables in the current scope:

Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
ans 1x19 19 char

Total is 25 elements using 67 bytes
>> v = [1 2 3 4 5]
v =

1 2 3 4 5

>> v1 = v(1:3) #select the first 3 elements in vector v
v1 =

1 2 3

>> save hello.mat v #save v to a file called hello.mat

>> save hello.txt v #save to text file

>> clear #clear all variables

>> A(3,2) #indexing row 3 col 2
ans = 6

>> A(2,:) #To fetch everything in the second row
ans =

3 4

>> A(:,2) #Everything in the second column
ans =

2
4
6

>> A([1 3], :) #Everything in row 1 and 3
ans =

1 2
5 6

>> A(:, 2) = [10; 11; 12] #Assign values to designated slots
A =

1 10
3 11
5 12

>> A = [A, [100; 101; 102]] #Append another column vector to the right
A =

1 10 100
3 11 101
5 12 102

>> A(:) #Put all elements of A into a single vector
ans =

1
3
5
10
11
12
100
101
102

>> A = [1 2; 3 4; 5 6;]
A =

1 2
3 4
5 6

>> B = [11 12; 13 14; 15 16]
B =

11 12
13 14
15 16

>> C = [A B] # Concatenate two matrices horizontally
C =

1 2 11 12
3 4 13 14
5 6 15 16

>> C = [A; B] #concatenate two matrices vertically
C =

1 2
3 4
5 6
11 12
13 14
15 16

>> A = [1 2; 3 4; 5 6;];
>> B = [11 12; 13 14; 15 16];
>> C = [1 1; 2 2];
>> A * C
ans =

5 5
11 11
17 17

>> A .* B #Element wise multiplication by two matrices
ans =

11 24
39 56
75 96

>> A .^ 2 #Element-wise squaring
ans =

1 4
9 16
25 36

>> 1 ./ v #Element-wise reciprocal
ans =

1.00000
0.50000
0.33333

>> log(v) #element-wise log
ans =

0.00000
0.69315
1.09861

>> exp(v) #element-wise exponentiation
ans =

2.7183
7.3891
20.0855

>> abs(v) #element-wise absolute value
ans =

1
2
3

>> -v #negative
ans =

-1
-2
-3

>> v + ones(length(v), 1) #increment v by 1 element-wise
ans =

2
3
4

>> v + 1
ans =

2
3
4

>> A' #Transpose'
ans =

1 3 5
2 4 6

>> (A')'
ans =

1 2
3 4
5 6

>> a = [1 15 2 0.5]
a =

1.00000 15.00000 2.00000 0.50000

>> val = max(a)
val = 15
>> [val, ind] = max(a)
val = 15
ind = 2 #index of the value in a

>> max(A) #Max value in each column
ans =

5 6

>> a < 3 #element wise comparison
ans =

1 0 1 1

>> find(a < 3) #returns index that satisfy condition
ans =

1 3 4

>> A = magic(3) #returns a matrix with all columns, rows and diagnals sum up to a same value
A =

8 1 6
3 5 7
4 9 2

>> [r, c] = find(A >= 7) #returns row and col index of the value that satisfy the condition
r =

1
3
2

c =

1
2
3

>> sum(a) #sum up all the elements
ans = 18.500
>> prod(a) #multiply all the elements
ans = 15
>> floor(a) #round down all the elements to the nearest integer (0.5 -> 0)
ans =

1 15 2 0

>> ceil(a) #round up the elements (0.5 ->1)
ans =

1 15 2 1

>> rand(3)
ans =

0.161777 0.929871 0.203446
0.619528 0.746753 0.051941
0.950696 0.178967 0.929934

>> max(rand(3), rand(3)) #take the element-wise maximum value of two 3*3 random matrices
ans =

0.93237 0.83219 0.87701
0.97407 0.82623 0.35239
0.69634 0.69960 0.60876

>> max(A, [], 1) #takes the column wise maximum. 1 here means to take the max among the first dimension of 8. This is the default return for max(A)
ans =

8 9 7

>> max(A, [], 2) #takes the row wise maximum
ans =

8
7
9

>> max(max(A)) #OR max(A(:)) To find the single maximum value in the whole matrix
ans = 9

>> A = magic(9)
A =

47 58 69 80 1 12 23 34 45
57 68 79 9 11 22 33 44 46
67 78 8 10 21 32 43 54 56
77 7 18 20 31 42 53 55 66
6 17 19 30 41 52 63 65 76
16 27 29 40 51 62 64 75 5
26 28 39 50 61 72 74 4 15
36 38 49 60 71 73 3 14 25
37 48 59 70 81 2 13 24 35

>> sum(A, 1) #Column wise sum up
ans =

369 369 369 369 369 369 369 369 369

>> sum(A, 2) #Row wise sum up
ans =

369
369
369
369
369
369
369
369
369

>> eye(9)
ans =

Diagonal Matrix

1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1

>> A .* eye(9)
ans =

47 0 0 0 0 0 0 0 0
0 68 0 0 0 0 0 0 0
0 0 8 0 0 0 0 0 0
0 0 0 20 0 0 0 0 0
0 0 0 0 41 0 0 0 0
0 0 0 0 0 62 0 0 0
0 0 0 0 0 0 74 0 0
0 0 0 0 0 0 0 14 0
0 0 0 0 0 0 0 0 35

>> sum(sum(A .* eye(9))) # sum up the diagnal
ans = 369
>> flipud(eye(9))
ans =

Permutation Matrix

0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
>> sum(sum(A .* flipud(eye(9)))) #sum up the other diagnal
ans = 369

>> A = magic(3)
A =

8 1 6
3 5 7
4 9 2

>> pinv(A) # Inverse of matrix A
ans =

0.147222 -0.144444 0.063889
-0.061111 0.022222 0.105556
-0.019444 0.188889 -0.102778

>> temp = pinv(A);
>> temp * A #all the value equals 0 except for the diagnal
ans =

1.0000e+00 8.3267e-17 -2.9421e-15
-5.9952e-15 1.0000e+00 6.3838e-15
2.9976e-15 4.4409e-16 1.0000e+00

>>

To load data into Octave

  1. cd to the file that contains the data files
  2. load filename.dat or load(‘filename.dat’)
  3. who

To visualize your data in Octave

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
>> t = [0:0.01:0.98];
>> y1 = sin(2*pi*4*t);
>> plot(t,y1) #t as x, y1 as y
>> y2 = cos(2*pi*4*t);
>> plot(t, y2)
>> hold on; #plot two y together
>> plot(t, y1);
>> plot(t, y2, 'r'); #color in red
>> plot(t, y1, 'b'); # color in blue
>> xlabel('time')
>> ylabel('value')
>> legend('sin', 'cos') #add legend
>> title('my plot') #add title
>> cd Desktop; print -dpng 'myPlot.png' #save the plot to designated place
>> close #close the plot window

>> figure(1); plot(t, y1) #two separate figures
>> figure(2); plot(t, y2)

>> subplot(1, 2, 1) #divides plot into a 1*2 grid, access the first element
>> plot(t, y1) #plot the figure at the first element place
>> subplot(1, 2, 2) #divides plot into a 1*2 grid, access the second element
>> plot(t, y2) #plot at the second element place
>> axis([0.5 1 -1 1]) # for the second plot, change x axis to (0.5, 1), y axis to (-1. 1)
>> clf #clears a figure to blank
>> imagesc(A), colorbar, colormap gray #convert matrix into colormap. colorbar shows the legend of colr. colormap gray defines to be black and white.

>> a=1, b=2, c=3 #comma chaining of commands
a = 1
b = 2
c = 3
>> a = 1; b = 2; c = 3; #same as above, except it does not print

Control statement: for, while, if

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
#for loop
>> v = zeros(10,1)
v =

0
0
0
0
0
0
0
0
0
0

>> for i = 1:10,
v(i) = 2^i;
end;
>> v
v =

2
4
8
16
32
64
128
256
512
1024

>> indices = 1:10;
>> for i = indices,
disp(i);
end;
1
2
3
4
5
6
7
8
9
10

# while loop
>> i = 1;
>> while i <= 5,
v(i) = 100;
i = i + 1;
end;
>> v
v =

100
100
100
100
100
64
128
256
512
1024

# break
>> i = 1;
>> while true,
v(i) = 999;
i= i+1;
if i == 6,
break;
end;
end;

>> v(1) = 2;
>> 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;
The value is two #since we have set v(1) = 2

If you want to exit Octave, you can just type exit and enter, it will cause Octave to quit.

To define a function in Octave, we need to creat a file in the working directory named functioname.m, and store the following definition in it. e.g.:

1
2
function y = squareThisNumber(x)
y = x^2

Then you can call directly in Octave

1
2
>> squareThisNumber(5)
ans = 25

We can also add a search path to Octave, so we don’t have to save the function file in the working directory

1
addpath('Users/joannaouyang/Desktop')

Octave allows you to return multiple values for a function

1
2
3
4
function [y1, y2] = squareAndCubeThisNumber(x)

y1 = x^2;
y2 = x^3;

Back to Octave

1
2
3
>> [a, b] = squareAndCubeThisNumber(5)
a = 25
b = 125

pre-defined cost function j

1
2
3
4
5
6
7
8
function J = costFunctionJ(X, y, theta)
# X is the 'design matrix' containing our training examples
# y is the class label

m = size(X, 1); # numberof training examples
predictions = X * theta; #predictions of hyputhesis on all m examples
sqrErrors = (predictions - y).^2; #squared errors
J = 1 / (2 * m) * sum(sqrErrors);

Go back to Octave

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>> x = [1 1; 1 2; 1 3]
x =

1 1
1 2
1 3

>> y = [1; 2; 3]
y =

1
2
3

>> theta = [0; 1];
>> j = costFunction(x, y, theta)
j = 0 #because with theta0 = 0, theta1 = 1, the line predict perfectly

Vectorization

To simplify the code and speed up running

1
2
3
4
5
6
7
8
# Unvectorized implementation
prediction = 0.0;
for j = 1:n+1,
prediction = prediction + theta(j) + x(j)
end;

# Vectorized implementation
prediction = theta' * x;

Gradient descent function vectorized implementation

1
2