Attachment 'MatrixAlgebra.m'
Download 1 %% Vector basics
2
3 %% a vector is an ordered sequence of numbers
4 x = [2 4 6 8]
5
6 y = [2:2:8]
7 z = [8:-2:2]
8
9 % getting an element out of a vector
10 y(3)
11
12 % basic operations
13 x/2
14 2*x
15 whos
16
17 % the number of elements of a vector
18 length(y)
19 % note: this is the number of elements, not the "Euclidean length"
20
21 % loop over a vector
22 for i=1:length(y)
23 2*y(i)
24 end
25
26 % plotting vectors
27 plot(y)
28 hold on % don't overwrite this
29 plot(z, 'r') % plot additional line in red colour
30
31 whos % look at your workspace
32 clear all % clear your workspace
33 whos % look at your (now empty) workspace
34 close all % close your figures
35
36 %% angle between vectors
37 x = [0 1];
38 y = [1 1];
39 plot([0 x(1)], [0 x(2)], 'r', [0 y(1)], [0 y(2)], 'k');
40 % what is the angle between these two vectors?
41
42 % cosine of angle between vectors
43 cosxy = x*y' / (norm(x)*norm(y));
44
45 % get the angle from cosine
46 acosxy = acos( cosxy )
47
48 % Why does is this not what we expected?
49 % tranform radians to degrees:
50 acosxy * (180/pi)
51
52
53 %% plotting functions of vectors
54
55 x = [-5:0.1:5]
56
57 % square every element of x
58 y = x.^2
59 plot(y)
60
61 % look at the x-axis of the plot - what's wrong?
62 % this is better
63 plot(x, y);
64 plot(x, y, 'x');
65
66 % a few more things
67 hold on
68 plot(x, (x-1).^2, 'r')
69 hold on
70 plot(x, x.^2+5, 'k')
71 plot(x, 2*(x-1).^2, 'g')
72
73 close all
74
75 % plot sine and cosine
76 sinx = sin(x);
77 cosx = cos(x);
78
79 plot(x, sinx, 'k', x, cosx, 'r')
80
81 % correlation
82 corr(sinx, cosx)
83 % produces lots of NaNs because dimension not appropriate, better:
84 corr(sinx', cosx')
85 % sine and cosine are orthogonal - why is this correlation not exactly 0?
86
87 %% Vector multiplication
88 % we can start afresh from here
89
90 % Define two parallel vectors:
91 x = [1 0 1 0]
92 y = [2 0 2 0]
93
94 % Multiply vectors
95 x*y % doesn't work...
96
97 % we have to "transpose" the vector on the right
98 x*y'
99
100 % transposing turns row (horizontal) vectors into column (vertical) vectors and vice versa
101 % this is because vectors are special types of matrices - we'll come to this later
102 x'
103
104 % now try
105 x'*y
106
107 % it really matters what you are doing! a times b is not always b times a!
108
109 %% Functions for two vectors
110
111 % correlation between two vectors
112 % try
113 corr(x, y)
114 % and you'll get a strange result
115 % the input should be column vectors, so do it like this
116
117 corr(x', y')
118
119 % ttest
120 ttest(x', y')
121 % produces a suspiciously significant result...
122 % check how the ttest works:
123 help ttest
124
125 % this produces the p-value
126 [h, p] = ttest(x', y')
127
128 % if you want the t-value:
129 [h, p, ci, stats] = ttest(x', y')
130 stats.tstat % "stats" is a structure, with multiple elements
131
132 % for simple measures
133 sum(x)
134 mean(x)
135 std(x)
136
137
138 %% Matrices
139
140 % Define a matrix
141 x = [1 1 1; 2 2 2]
142
143 % Getting elements out of matrices
144 x(1,2)
145 x(:,1)
146 x(2,:)
147
148 % Getting matrix dimensions
149 size(x) % returns number of rows and columns, respectively
150 [m,n] = size(x)
151
152 %% Matrices c'd
153 % Matrix transpose
154 x
155 y = x'
156
157 % visualise a matrix
158 imagesc(x)
159 imagesc(y)
160
161 % adding/multiplying values etc.
162 x + 2
163 2 * x
164 x.^2
165
166 % Define another matrix
167 z = [1 2 3; 4 5 6]
168
169 % Subtract z from x
170 x - z
171
172 % Add z to x
173 x + z
174
175 % !!! Elementwise multiplication (NOT "matrix multiplication": "*")
176 x .* z
177
178 % Try... (matrix multiplication)
179 x * z
180 % doesn't work because dimensions don't match (we'll get to this later)
181
182 % Elementwise division (NOT "matrix division/inversion": "/")
183 x ./ z
184
185 % Try... (matrix inversion)
186 x / z
187
188 %% Multiplying matrix and vector
189 % Define a vector
190 v = [1 1 1]'
191
192 % Mutliply matrix x with vector v (and obtain sum within each row of x)
193 x
194 v
195 x * v
196
197 % matrix and vector dimensions must agree
198 v * x % does not work
199
200 %% interpretation as sum of column vectors
201 % x * v results in the sum of column vectors in x
202 x * v
203 % produces the same result as
204 1*x(:,1) + 1*x(:,2) + 1*x(:,3)
205
206 %% Matrix multiplication
207 % Multiply two matrices
208 x * y
209
210 % Multiply two matrices
211 y * x
212
213 % !!! Note: x*y is not necessarily y*x!!!
214
215 % Define new matrix
216 z = [1 1; 2 2]
217
218 % Works:
219 y*z
220 z*x
221
222 % Doesn't work:
223 z*y
224 x*z
225
226 %% Make your own matrices
227
228 % Identity matrix
229 id = eye(3)
230
231 % Example:
232 x*id
233
234 id*x'
235
236 % diagonal matrix
237 di = eye(3)
238 di(1,1) = 1
239 di(2,2) = 2
240 di(3,3) = 3
241
242 % Multiplies columns by a factor:
243 x*di
244
245 % Multiplies rows by a factor
246 di*y
247
248 %% Some useful functions for matrices
249
250 % sum/average across columns of a matrix
251 % note: most Matlab function operate column-by-column
252 sum( x )
253 mean( x )
254
255 % sum across rows of a matrix
256 sum( x' )
257 mean( x' )
258
259 % get the diagonal of a (square) matrix
260 mydiag = diag(di)
261
262 % dimensions of a matrix, size()
263 [m,n] = size(x)
264
265 % dimension of a vector
266 diaglen = length(mydiag)
267
268 % create a matrix/vector with ones
269 ones(10) % creates 10x10 matrix
270 ones(10,1) % creates 10x1 column vector
271
272 % create a matrix/vector with zeros
273 zeros(10) % creates 10x10 matrix
274 zeros(1,10) % creates 1x10 row vector
275
276 % create matrix/vector with standard uniform random numbers in interval 0-1
277 rand(3)
278
279 % create matrix/vector with standard normal random numbers in interval 0-1
280 randn(3)
281
282 % correlation among columns of a matrix
283 R = rand(5,3)
284 % correlation among columns
285 corr(R)
286 % correlation among rows
287 corr(R')
288 % visualise a matrix
289 imagesc( corr(R') )
290 colorbar
291
292
293 % "reshaping" a matrix or a vector
294 x = 1:10
295 reshape(x,5,2)
296 reshape(x,2,5)
297
298 % Kronecker product ("concatenates" matrices/vectors)
299 kron(id,y)
300
301 %% EMEG example
302 load EMEGdata.mat
303 whos
304 data
305 data.evoked
306 data.evoked.epochs
307
308 % get only EEG data
309 mydat = data.evoked.epochs(307:376,:)
310 plot( mydat' )
311
312 % find bad channel
313 max(max(mydat))
314 max(max(mydat'))
315 [a,b] =max(max(mydat'))
316
317 % remove bad channel
318 mydat(63,:) = []
319 whos
320
321 plot( mydat' )
322
323 [m,n] = size( mydat );
324
325 % create "average reference operator"
326 avg_op = (eye(m)-ones(m)/m);
327
328 % this subtracts the mean across electrodes at each sample (column)
329 mydatref = (eye(69)-ones(69)/69)*mydat;
330
331 % this should look different:
332 plot( mydatref' )
333
334 % a different way of plotting 2D data
335 imagesc(mydatref)
336 colorbar
337
338 % plot correlation matrix among electrodes
339 imagesc(corr(mydatref'))
340 colorbar
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.