-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlcv_tutorial.m
More file actions
284 lines (222 loc) · 3.64 KB
/
mlcv_tutorial.m
File metadata and controls
284 lines (222 loc) · 3.64 KB
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
%%%%%%%% Machine Learning for Computer Vision %%%%%%%%
%%%%%%%% Matlab Tutorial %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% VECTORS %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
%% define a 2-dimensional vector
x = [9 4];
%% define a 6-dimensioanl vector
y = [2 4 6 2 1 5]
%% transpose of a vector
y'
%% create vector from a pre-defined pattern
z = [1:5]
%% or
w = [1:0.5:4]
%% access specific element of vector
w(3)
z(3)
%% create nameless vector
0:1:10
%% transpose it
ans'
%% or
(0:1:10)'
%% access sub-vector
w(1:3)
%% or
w(1:3:6)
%% perform operations
w(1:3)-w(2:4)
%% stepping back
y = [0:-1:-6]
%% w + y
w+y
%% -2*y
-2*y
%% w/3
w/3
%% 3*w - [5 4]
3*w - [5 4]
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% MATRICES %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%% clear all, close all, clc
%clear all
%close all
%clc
%% Define 3x3 matrix
A = [ 1 2 3; 3 4 5; 6 7 8]
%% Define 3x3 matrix as combination of vectors
B = [[1 4 7]' [2 5 8]' [3 6 9]']
%% You've lost track?
whos
%% Multiplying
x = [1:3:9]
A*x
%% But
A*x'
%% Slicing
A(1:2,3:4)
%% But
A(1:2,2:3)
%% Transpose
A(1:2,2:3)'
%% Access row and columns
A(1,:)
A(:,1)
%% Diagonal
diag(A)
%% create random 3x3 matrix
A = rand(3,3)
%% Determinant
det(A)
%% Furthermore Bx=v find x
v = [1 3 5]'
B = [[1 3 5]' [2 4 8]' [4 6 8]']
x=B\v %%solves Bx=v
B*x
%% or?...
x1 = v'/B %%solves xB=v
x1*B
%%%%%%%%%%%%%%%%%%%%%
%%%%%%% PLOTS %%%%%%%
%%%%%%%%%%%%%%%%%%%%%
%% clear all, close all, clc
clear all
close all
clc
%% example
x = 0:2*pi/100:2*pi;
y = sin(x);
z = cos(x);
size(y)
max(size(y))
y(1)
plot(x,y)
grid on
%% example subplot
subplot(1,2,1);
plot(x,y)
grid on;
subplot(1,2,2);
plot(x,z)
grid on;
%% print result
print -dps plot.ps
%% more examples - contour
[c,h] = contour(peaks); clabel(c,h), colorbar
%% more examples - histogram
x = -4:0.1:4; y = randn(10000,1); hist(y,x)
%% more examples - surface 3D plot
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z)
%%%%%%%%%%%%%%%%%%%%%
%%%%%%% LOOPS %%%%%%%
%%%%%%%%%%%%%%%%%%%%%
%% clear all, close all, clc
clear all
close all
clc
%% for
for j=1:4
j
end
%% or
v = [1:3:10]
for j=1:3
v(j)=3*j;
end
v
%% operations on the rows of a matrix
A = [ [1 2 3]' [3 2 1]' [2 1 3]']
B = A;
for j=2:3
A(j,:) = A(j,:) - A(j-1,:)
end
%% more examples
for j=2:3,
for i=j:3,
B(i,:) = B(i,:) - B(j-1,:)*B(i,j-1)/B(j-1,j-1)
end
end
%% while loops
k = 100
while (k < 101) && (k>50)
k = k / 1.5
end
%% vectorize always
N = 1000;
A = rand(N);
tic() % start time counter
d1 = diff(A);
t1 = toc() % stop timer counter
tic()
for i = 1:N-1
d2(i,:) = A(i+1,:) - A(i,:);
end
t2 = toc()
fprintf(1,'delay: %f%%\n', 100*t2/t1);
tic()
for i = 1:N-1
for j = 1:N
d3(i,j) = A(i+1,j) - A(i,j);
end
end
t3 = toc()
fprintf(1,'Delay: %f%%\n', 100*t3/t1);
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% FUNCTIONS %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
%% clear all, close all, clc
clear all
close all
clc
%% first create and then call
myfunc([-1 1 2 3])
r = myfunc([-1 1 2 3])
[r1, r2] = myfunc([-1 1 2 3])
%%%%%%%%%%%%%%%%%%%%
%%%%%%% DATA %%%%%%%
%%%%%%%%%%%%%%%%%%%%
%% clear all, close all, clc
clear all
close all
clc
%% saving
x = [1 2 3];
y = [6 5 4];
whos
save vars.mat
dir
%% loading
clear
whos
load vars.mat
whos
%% loading whatever you want
clear
whos
load vars.mat x
whos
%% deleting
delete vars.mat
dir
%% save session
diary save.txt
r = 5;
z = 2;
diary
%%%%%%%%%%%%%%%%%%%%%
%%%%%%% FILES %%%%%%%
%%%%%%%%%%%%%%%%%%%%%
%% clear all, close all, clc
clear all
close all
clc
%% create temp.log offline and then
fp = fopen('temp.log','r');
tmp_10 = fscanf(fp,'%d,%d',10);% read 10 items
tmp_all = fscanf(fp, '%d,%d') %read all items
fclose(fp)