forked from NINC-UBC/MATLABTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycle6_week2.m
More file actions
239 lines (165 loc) · 8.84 KB
/
cycle6_week2.m
File metadata and controls
239 lines (165 loc) · 8.84 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
% These examples are adapted from the YAGTOM (yet another guide to MATLAB)
% section "Matrix Operations"
% Matrices are key in matlab as we will use them to store most types of
% data. For example an image can be represented as a 2d matrix and a time
% course of a signal can be represeted by a 1d matrix.
% Our goals are to introduce commands which generate matrices with different
% properties (eg ones, zeroes, randn, rand, etc)
% and those that report properties of the matrices (ie, number of elements
% or the mean value, standard deviation). We also learn to isolate specific
% elements of a matrix. This is known as indexing.
% http://ubcmatlabguide.github.io/html/matrixOperations.html
% First a review from week 1: the semi colon
% If you have got a handle on ; skip to line 61
% Recall "Bonus: run the same command again with a semi colon symbol ; at the end
% of the command. What does the semi colon do?"
% Explanation: I wanted to point out the semi colon early on as a lot of people
% find its use confusing when beginning to use Matlab. Common questions arise:
% "Do I need to put a semi colon here??"
% The role of a semi colon when following a command is to prevent matlab from
% displaying the result of a command in the command window. Recall the ones
% command which gives a matrix filled with ones. Evaluate:
a=ones(2,2)
% In this case the variable a is assigned a 2 row x 2 column matrix
% filled with ones. It should now be in your workspace. And notice the
% ones are printed in the command window.
% Evaluate:
clear a
a=ones(2,2);
% The command clear a deletes a from the workspace. The next line creates a and
% assigns it ones again. You will see it in the worksapce, but if you look
% in the command window, the values have not been displayed.
% The semi colon suppresses output in the command window when assigning
% values to variables.
% In general evaluating commands without a semi colon is helpful when you
% are checking your work and need to see a particular variable's value or a
% single element or a few elements of a matrix. For example you might need
% to check the value in one of the elements of a. Evaluate:
a(1,2)
% However, if you are writing a script to automate your data analysis and
% values will be assigned to variable a 100's of 1000's of times, you probably want
% the semi colon as displaying them all to the screen is not helpful. (NB.: Another
% role for semi colons is to separate rows of numbers when manually
% entering matrices. We will see this below.)
% Ok, enough about ; ... on to creating matrices.
% As we saw last time as well, variables can be created directly by
% assigning them values using the = operator. This can be a single
% numerical value, for example evaluate:
a=1
% Or a matrix, for example: (NB: Here ; is used to separate rows of the
% matrix elements. It's not often we manually type numbers in this way,
% but it is useful for examples.)
a=[1 2 3; 4 5 6 ; 7 8 9]
% An important shorthand exists in matlab for generating matrices which
% contain lists of numbers which can be generated by a simple rule.
% A typical command looks like this. Evaluate:
a=1:10
% This assigns a a list of numbers starting at 1 and ending with 10.
% Incrementing by 1. Plus 1 is the default increment, but you can specify
% it. A typical command looks like:
a=1:.2:5
% which means start at 1 and go to 5 in steps of 0.2, the increment appears
% between the start and end, separated by the colons. Negative increments
% are possible as well.
% 1. Write a command to assign a list of numbers starting at 4 ending at -7
% decrementing by 1 each time.
% matlab also has many built in functions which create matrices for you.
% We introduced zeros and ones last time. Try these:
a=zeros(3,4)
b=ones(4,3)
% There is also randn which fills the matrix with random, normally distributed
% numbers. We will use this in many examples during the tutorials.
a=randn(2,5)
% and its counterpart rand which generates random numbers distributed
% uniformly between 0 and 1.
b=rand(5,2)
% 2. Use randn to create a matrix called dat of size 1000 columns in a single row.
% We will use this matrix in later examples.
dat = randn(1,10000);
% matlab has many useful built in functions to give you properties of your
% matrices or their contents.
% For example if we needed to know the number of elements we'd stored in
% dat we can use numel:
npts=numel(dat)
% in this command dat is given as the input argument between the
% parenthesis to the function numel. numel calculates the number of
% elements and the result is assigned to the variable npts by the =
% size is another very useful function.
% 3. Give dat as the input argument of size in an analogous fashion to
% the command for numel above. Assign the result to a variable called s.
% make sure to leave the ; out so you can see the result directly in the
% command window.
% If you get stuck try:
doc size
% to see examples in the documentation
% As you can see the result (check the workspace) s is itself a matrix.
% In this instance it has 2 elements the first should be equal to 1
% (the number of rows) and the second to 1000 (the number of columns of dat).
% It generalizes to more than 2 dimensions as well.
% 4. Try size on a multi-dimensional matrix. Use the command randn in an
% analogous fashion to the example above to create a matrix with 3 dimensions.
% Make the first dimension have 3 possible values the second 4 and the
% third 5. Assign this to a variable called test3d. Run size on test3d.
% Assign the result to s again overwriting the previous result.
% Matlab also has built in functions to calculate for you the mean and
% standard deviation of values stored in your matrices.
% 5. Use the built-in matlab functions mean and std to calculate the standard
% deviation and mean of the values you stored in dat above. Store the
% results in the variables dat_std and dat_avg respectively.
% Check doc mean and doc std for help
% Bonus1: Evaluate the following command to generate a figure of a histogram of the
% values stored in dat. This will give you a graphical representation
% of the distribution of the random numbers. We will spend more time on
% figures and plots in week 5.
figure;hist(dat)
% Do the values you get for dat_std and dat_avg make sense based on the
% properties of samples drawn randomly from a standard normal distribution?
% Bonus2: Write a command to generate 1000 normally distributed random numbers
% with a mean of 5 and standard deviaiton of 2. Store the result in the
% variable named dat2.
% Use an analogous command to the figure command above to view a histogram
% of the values in dat2.
% Use commands mean and std to check that dat2 has the correct properties.
% Hint: Check the following website to find more information on means and
% and standard deviations: http://www.stat.yale.edu/Courses/1997-98/101/rvmnvar.htm
% Matrices can be combined by concatenation. For example if we create two
% matrices by evaluating these commands:
a=[1 2 3]
b=4:10
% but we want the list of numbers stored in a and b in a single variable,
% we could use the command:
c=[a b]
% Alternatively we can use the built-in matlab function cat. It is useful
% in particular for multidimensional matrices.
c=cat(2,a,b)
% Cat requires 3 input arguments. The first is the dimension in which we
% are combining in this case columns (2). The other two arguments are the 2
% matrices we are combining.
% We will now move on to indexing. Indexing is very important as it is how
% we specify subsets of the elements of our matrices to work with. These
% may correspond to a region of interest in an image or a specific window
% in time in a time course of a signal.
% For example if we wanted to see the first element of our normally
% distributed random numbers stored in dat we could use
dat(1)
% The matrix's variable name is followed by the index we want (in this case
% 1) in parenthesis.
% We can also use a range of numbers using the shorthand we discussed
% above. For instance if we wanted to see the first 10 elements
dat(1:10)
% or elements 15 through 25
dat(15:25)
% Notice that these subsets of dat that we obtain by specifying the
% indices are themselves matrices. The results are stored in the default
% variable ans that we discussed in week 1.
% We can assign them to a new variable name using the equals sign if we
% need to use them later.
% 6. Imagine that dat actually represents a time course of a signal
% (EEG voltage or Ca2+ fluorescence for example) in response to a stimulus.
% Imagine that the time course is divided into a baseline and a response.
% The baseline consists of the first 250 time points. Write a command to
% isolate the baseline by indexing and storing the result in a new variable
% called base_line.
% Bonus1: Use mean to compute the mean of the baseline time points. Is it
% close to the value you might expect given the properties of the values
% generated by randn?