MATLAB Tutorial - CCN Course 2012: How To Code A Neural Network Simulation
MATLAB Tutorial - CCN Course 2012: How To Code A Neural Network Simulation
Malte J. Rasch
Overview
Why MATLAB ?
Pro:
Matrix-like numerical operations very fast and easy to use
Good plotting capabilities
Script language for programming small to medium sized
problems in applied mathematics (rapid prototyping)
Widely used in the neuroscience community for data analysis
as well as computational projects
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Why MATLAB ?
Pro:
Matrix-like numerical operations very fast and easy to use
Good plotting capabilities
Script language for programming small to medium sized
problems in applied mathematics (rapid prototyping)
Widely used in the neuroscience community for data analysis
as well as computational projects
Contra:
Support of symbolic/analytic expressions less advanced
Mathematica, Maple
Often not flexible/ fast enough for big projects
e.g. Python much more versatile
specialized software for detailed/large neural network
simulations
(NEURON, PCSIM, NEST, GENESIS, ...)
Very expensive, especially when using on a cluster
free alternative: (scientific) python
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Starting MATLAB
Scalar expressions
Binary operations: work as expected, use = + - * / ^.
a2 x
Example (compute y = 2+a + b)
>> a = 2;
>> b = 1;
>> x = 0.5;
>> y = a^2*x/(2+a) + b;
>> y
y =
1.500
Unary operations: called as functions with (), eg.
sin cos tan atan sqrt log gamma
√
sin x
Example ( compute y = ln x )
>> x = 0.5;
>> y = sqrt(sin(x))/log(x);
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Arrays: Overview
straight-forward
however, carefully check
the size of matrices
if element-wise or matrix-like operations are intended
which matrix dimension to operate on
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
straight-forward
however, carefully check
the size of matrices
if element-wise or matrix-like operations are intended
which matrix dimension to operate on
Indexing arrays
1 Subscript of a matrix:
access the (i, j)-th element of a 2D-matrix A of dimension (m, n)
>> A(i,j)
Indexing arrays
1 Subscript of a matrix:
access the (i, j)-th element of a 2D-matrix A of dimension (m, n)
>> A(i,j)
Indexing arrays
1 Subscript of a matrix:
access the (i, j)-th element of a 2D-matrix A of dimension (m, n)
>> A(i,j)
>> A(1:4,:)
>> B(:,:,2)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
>> A(1:4,:)
>> B(:,:,2)
5 Logical indexing
logical matrices of the same size as A can be used as indices
>> A(A>0)
>> A(find(A>0))
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
m-file scripts
m-file scripts
m-file scripts
m-file scripts
m-file scripts
m-file scripts
m-file scripts
Reminder:
1 Open a text-editor
>> myscript;
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
m-file functions
m-file functions
m-file functions
m-file functions
m-file functions
Note: The variable scope is different from that of the caller. That
means, that variables defined in the function body are NOT
present in the workspace after calling the function
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
m-file functions
Note: The variable scope is different from that of the caller. That
means, that variables defined in the function body are NOT
present in the workspace after calling the function
Note 2: Input arguments are always referenced by value
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
m-file functions
Note: The variable scope is different from that of the caller. That
means, that variables defined in the function body are NOT
present in the workspace after calling the function
Note 2: Input arguments are always referenced by value
Note 3: When called the m-file file name is used.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
m-file functions
Reminder:
In the first line input and output arguments are defined:
function outarg = myfun(inarg1,inarg2,...);
Call the function with
>> result = myfun(p1,p2,...);
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Basic syntax
Example (if-else)
a = rand ( 1 ) ;
i f a == 0 . 5
f p r i n t f ( ’ you a r e v e r y l u c k y ! ’ ) ;
end
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
a =0;
for i = 1:100
a = a+i ;
end
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Avg. response
K
θ,ρ,ω (+ phase invariance) 1.5
1
θ
2
y
y
0.5
θ3 0
t vary θ t
0° 90° 180°
:
x x
Vert. RF
→ Movement dir. →
→ Movement dir. →
10
5
TF [Hz]
−5
Response
2
−10 0
2 0 −2
−2
SF [cyc/deg] SF [cyc/deg]
° ° ° ° ° ° ° ° °
0 90 180 0 90 180 0 90 180
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Overview
Overview
How to plot
How to plot:
1 Open a figure (window) with
>> figure;
2 Create an axes object with
>> axes;
3 Type the plotting command of your choice
>> plot(x,y,’b--’,’LineWidth’,2);
Note: graphics commands draw into the current axes (gca) on the current
figure (gcf). They automatically create a new figure and axes if necessary.
4 Make the plot nicer by adding labels and setting limits, eg:
>> xlabel(’Time [s]’);
>> ylabel(’Response [spks/sec]’);
>> xlim([-1,1]); ylim([-2,2]);
>> title(’Simulation’)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
plot command
Basic syntax:
handle = plot(X,Y,linespec,optname1,val1,...);
1 figure ;
2
3 % p l o t G a u s s i a n PDF
4 x = −4:0.01:4; 0.45
5 y = exp ( −0.5∗ x . ˆ 2 ) / s q r t ( 2 ∗ p i ) ; 0.4
true PDF
empirical PDF
6 p l o t ( x , y , ’−k ’ , ’ L i n e w i d t h ’ , 2 ) ;
0.35
7
0.3
8 % e m p i r i c a l pdf
Probability density
0.25
9 % h i s t o g r a m o f 1000 random v a l u e s
10 [ N, z ]= h i s t ( r a n d n ( 1 0 0 0 , 1 ) ) ; 0.2
11 %n o r m a l i z a t i o n 0.15
13 0.05
14 h o l d on ; %a d d s a l l f o l l o w i n g g r a p h s 0
−4 −3 −2 −1 0 1 2 3 4
Random variable x
15 %t o t h e c u r r e n t a x e s
16 plot ( z , p , ’ rx ’ , ’ MarkerSize ’ ,10);
17 hold o f f ;
18
19 x l a b e l ( ’ Random v a r i a b l e x ’ )
20 ylabel ( ’ Probability density ’ )
21 l e g e n d ( ’ t r u e PDF ’ , ’ e m p i r i c a l PDF ’ )
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Goal of tutorial
Writing scripts
Usage of array notation
How to integrate ODEs
How to plot results
How to simulate neurons and synapses
How to program a quite realistic network simulation
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
dVm 1 X
Cm =− (Vm − VL ) − gi (t)(Vm − Ei ) + Isyn + Iapp
dt Rm
i
dVm 1 X
Cm =− (Vm − VL ) − gi (t)(Vm − Ei ) + Isyn + Iapp
dt Rm
i
Neuron model
peak 30 mV
RZ RS
v'= 0.04v 2+5v +140 - u + I LTS,TC
8
parameter d
0.25
parameter b
u'= a(bv - u)
reset c IB
RS,IB,CH FS 4
v(t) 0.2
if v = 30 mV, de
cay FS,LTS,RZ CH
reset d with r 2
then v c, u u + d ate a
0.05 TC
u(t)
sensitivity b 0 0.02 0.1 -65 -55 -50
parameter a parameter c
regular spiking (RS) intrinsically bursting (IB) chattering (CH) fast spiking (FS)
v(t)
I(t)
20 mV
40 ms
-63 mV
-87 mV
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercise 1
Simulate the neuron model for 1000ms and plot the resulting
voltage trace. Apply a current step (Iapp = 7pA) between time
200ms and 700ms.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercise 1
Simulate the neuron model for 1000ms and plot the resulting
voltage trace. Apply a current step (Iapp = 7pA) between time
200ms and 700ms.
Neuron model:
v̇ = (0.04v + 5) v + 140 − u + Iapp
u̇ = a (b v − u)
v ← c, u ← u + d, if v ≥ 35
Step 1 in detail:
Open MATLAB HowTo and create a new file (script) that will simulate the neuron
HowTo . If you do not know how to use a command get some help HowTo
Proceed as follows:
1 Initialize parameter values (∆t = 0.5ms, a = 0.02, d = 8, · · · )
HowTo
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 1 in detail:
Open MATLAB HowTo and create a new file (script) that will simulate the neuron
HowTo . If you do not know how to use a command get some help HowTo
Proceed as follows:
1 Initialize parameter values (∆t = 0.5ms, a = 0.02, d = 8, · · · )
HowTo
Step 1 in detail:
Open MATLAB HowTo and create a new file (script) that will simulate the neuron
HowTo . If you do not know how to use a command get some help HowTo
Proceed as follows:
1 Initialize parameter values (∆t = 0.5ms, a = 0.02, d = 8, · · · )
HowTo
Step 1 in detail:
Open MATLAB HowTo and create a new file (script) that will simulate the neuron
HowTo . If you do not know how to use a command get some help HowTo
Proceed as follows:
1 Initialize parameter values (∆t = 0.5ms, a = 0.02, d = 8, · · · )
HowTo
Step 1 in detail:
Open MATLAB HowTo and create a new file (script) that will simulate the neuron
HowTo . If you do not know how to use a command get some help HowTo
Proceed as follows:
1 Initialize parameter values (∆t = 0.5ms, a = 0.02, d = 8, · · · )
HowTo
Step 1 in detail:
Open MATLAB HowTo and create a new file (script) that will simulate the neuron
HowTo . If you do not know how to use a command get some help HowTo
Proceed as follows:
1 Initialize parameter values (∆t = 0.5ms, a = 0.02, d = 8, · · · )
HowTo
Step 1 in detail:
Open MATLAB HowTo and create a new file (script) that will simulate the neuron
HowTo . If you do not know how to use a command get some help HowTo
Proceed as follows:
1 Initialize parameter values (∆t = 0.5ms, a = 0.02, d = 8, · · · )
HowTo
Exercise 2
Simulate the neuron model for 1000ms and plot the resulting
voltage trace. Assume that 100 synapses are attached to the
neuron, with each pre-synaptic neuron firing with a Poisson
process of rate frate = 2 Hz between time 200ms and 700ms.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercise 2
Simulate the neuron model for 1000ms and plot the resulting
voltage trace. Assume that 100 synapses are attached to the
neuron, with each pre-synaptic neuron firing with a Poisson
process of rate frate = 2 Hz between time 200ms and 700ms.
Step 2 in detail:
Use the last script, save it under a new file name, and add the necessary lines.
Proceed as follows:
1 Initialize new parameter values (τg = 10, frate = 0.002ms−1 )
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 2 in detail:
Use the last script, save it under a new file name, and add the necessary lines.
Proceed as follows:
1 Initialize new parameter values (τg = 10, frate = 0.002ms−1 )
2 Reserve memory and initialize gin = (gjin ), win = (wjin ), and
E = (Ej ) (vectors of length nin = 100) with constant elements
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 2 in detail:
Use the last script, save it under a new file name, and add the necessary lines.
Proceed as follows:
1 Initialize new parameter values (τg = 10, frate = 0.002ms−1 )
2 Reserve memory and initialize gin = (gjin ), win = (wjin ), and
E = (Ej ) (vectors of length nin = 100) with constant elements
3 Inside the for-loop change/add the following:
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 2 in detail:
Use the last script, save it under a new file name, and add the necessary lines.
Proceed as follows:
1 Initialize new parameter values (τg = 10, frate = 0.002ms−1 )
2 Reserve memory and initialize gin = (gjin ), win = (wjin ), and
E = (Ej ) (vectors of length nin = 100) with constant elements
3 Inside the for-loop change/add the following:
1 set pj = 1 if r ≤ frate ∆t (otherwise 0) in the case when i∆t is
between 200 and 700 (otherwise 0). r = (rj ) is a vector of
uniform random numbers of length nin
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 2 in detail:
Use the last script, save it under a new file name, and add the necessary lines.
Proceed as follows:
1 Initialize new parameter values (τg = 10, frate = 0.002ms−1 )
2 Reserve memory and initialize gin = (gjin ), win = (wjin ), and
E = (Ej ) (vectors of length nin = 100) with constant elements
3 Inside the for-loop change/add the following:
1 set pj = 1 if r ≤ frate ∆t (otherwise 0) in the case when i∆t is
between 200 and 700 (otherwise 0). r = (rj ) is a vector of
uniform random numbers of length nin
2 before the vt update: implement the conductance dynamics g
and set Iapp (using array notation HowTo carefully HowTo ):
gjin ← gjin + pj
win · gin ⊙ Ein − win · gin ⊙ vt
Iapp ←
gjin ← (1 − ∆t/τg ) gjin
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercise 3
Simulate 1000 neurons for 1000 ms and plot the resulting spikes.
Assume that each neuron receives (random) 10% of the 100
Poisson spike trains of rate frate = 2 Hz between time 200 ms and
700 ms. Note that the neurons are not yet inter-connected.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercise 3
Simulate 1000 neurons for 1000 ms and plot the resulting spikes.
Assume that each neuron receives (random) 10% of the 100
Poisson spike trains of rate frate = 2 Hz between time 200 ms and
700 ms. Note that the neurons are not yet inter-connected.
Step 3 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize new parameter values (n = 1000)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 3 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize new parameter values (n = 1000)
2 Initialize 2 logical vectors (for indexing HowTo ) kinh and kexc of
length n, where kinh has a 1 in element i with probability p = 0.2
(marking an inhibitory neuron) and 0 otherwise. kexc = ¬kinh .
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 3 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize new parameter values (n = 1000)
2 Initialize 2 logical vectors (for indexing HowTo ) kinh and kexc of
length n, where kinh has a 1 in element i with probability p = 0.2
(marking an inhibitory neuron) and 0 otherwise. kexc = ¬kinh .
3 Reserve memory and initialize vij , uij (now being n × T matrices).
Set parameters ai and di according to kexc and kinh .
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 3 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize new parameter values (n = 1000)
2 Initialize 2 logical vectors (for indexing HowTo ) kinh and kexc of
length n, where kinh has a 1 in element i with probability p = 0.2
(marking an inhibitory neuron) and 0 otherwise. kexc = ¬kinh .
3 Reserve memory and initialize vij , uij (now being n × T matrices).
Set parameters ai and di according to kexc and kinh .
4 The weights wijin = 0.07 now form a n × nin matrix. Set 90 %
random elements to 0 to account for the connection probability.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 3 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize new parameter values (n = 1000)
2 Initialize 2 logical vectors (for indexing HowTo ) kinh and kexc of
length n, where kinh has a 1 in element i with probability p = 0.2
(marking an inhibitory neuron) and 0 otherwise. kexc = ¬kinh .
3 Reserve memory and initialize vij , uij (now being n × T matrices).
Set parameters ai and di according to kexc and kinh .
4 The weights wijin = 0.07 now form a n × nin matrix. Set 90 %
random elements to 0 to account for the connection probability.
5 Inside the for-loop change/add the following:
1 Same update equations (for vi,t+1 and ui,t+1 ) but use array
notation to update all i neurons simultaneously.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 3 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize new parameter values (n = 1000)
2 Initialize 2 logical vectors (for indexing HowTo ) kinh and kexc of
length n, where kinh has a 1 in element i with probability p = 0.2
(marking an inhibitory neuron) and 0 otherwise. kexc = ¬kinh .
3 Reserve memory and initialize vij , uij (now being n × T matrices).
Set parameters ai and di according to kexc and kinh .
4 The weights wijin = 0.07 now form a n × nin matrix. Set 90 %
random elements to 0 to account for the connection probability.
5 Inside the for-loop change/add the following:
1 Same update equations (for vi,t+1 and ui,t+1 ) but use array
notation to update all i neurons simultaneously.
6 Plot the spike raster. Plot black dots at {(t, i)|vit ≥ 35} for
excitatory neuron i. Use red dots for inhibitory neurons.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercise 4
Simulate 1000 neurons as before but with added recurrent
connections.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercise 4
Simulate 1000 neurons as before but with added recurrent
connections.
Step 4 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize and allocate memory for the new variables ( g = (gj ), Ej ).
Set Ej = −85 if j is an inhibitory neuron (otherwise 0).
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 4 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize and allocate memory for the new variables ( g = (gj ), Ej ).
Set Ej = −85 if j is an inhibitory neuron (otherwise 0).
2 Reserve memory and initialize weights W = (wij ) to zero.
Randomly choose 10% of the matrix elements.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 4 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize and allocate memory for the new variables ( g = (gj ), Ej ).
Set Ej = −85 if j is an inhibitory neuron (otherwise 0).
2 Reserve memory and initialize weights W = (wij ) to zero.
Randomly choose 10% of the matrix elements.
3 Set the chosen weight matrix elements to values drawn from a
Gamma distribution of scale 0.003 and shape 2 HowTo
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 4 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize and allocate memory for the new variables ( g = (gj ), Ej ).
Set Ej = −85 if j is an inhibitory neuron (otherwise 0).
2 Reserve memory and initialize weights W = (wij ) to zero.
Randomly choose 10% of the matrix elements.
3 Set the chosen weight matrix elements to values drawn from a
Gamma distribution of scale 0.003 and shape 2 HowTo
4 Make the weight matrix “sparse” HowTo
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Step 4 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize and allocate memory for the new variables ( g = (gj ), Ej ).
Set Ej = −85 if j is an inhibitory neuron (otherwise 0).
2 Reserve memory and initialize weights W = (wij ) to zero.
Randomly choose 10% of the matrix elements.
3 Set the chosen weight matrix elements to values drawn from a
Gamma distribution of scale 0.003 and shape 2 HowTo
4 Make the weight matrix “sparse” HowTo
Step 4 in detail:
Modify the last script (after saving it under new name).
Proceed as follows:
1 Initialize and allocate memory for the new variables ( g = (gj ), Ej ).
Set Ej = −85 if j is an inhibitory neuron (otherwise 0).
2 Reserve memory and initialize weights W = (wij ) to zero.
Randomly choose 10% of the matrix elements.
3 Set the chosen weight matrix elements to values drawn from a
Gamma distribution of scale 0.003 and shape 2 HowTo
4 Make the weight matrix “sparse” HowTo
gj ← gj + 1, if vj (t − 1) ≥ 35
syn
I ← W · (g ⊙ E) − (W · g) ⊙ v
gj ← (1 − ∆t/τg ) gj
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Congratulation !
Optional exercises
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercises
Exercise (p-series)
Calculate the p-series (generalization of the Harmonic Series) for a
given p up to a given m
m
X 1
µm =
np
i=1
Advise: Use array notation and avoid for-loops whereever you can!
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercises
Exercises
Exercises
Advise: Use array notation and avoid for-loops whereever you can!
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises
Exercises
Exercises
Exercise (Plot 2-D Gaussian point cloud)
Write a function which plots a point cloud of n random
samples drawn from a 2D Normal distribution (with 0 5
mean) and variance Σ = (RD)T (RD), where the
0
rotation matrix is defined as
-5
cos θ − sin θ
R=
sin θ cos θ -5 0 5
Advanced exercise
back
Howtos
Alternatively:
http://www.mathworks.com/access/helpdesk/help/techdoc/
back
Howtos
3 Make the plot nicer by adding labels and setting limits, eg:
Example
>> a = 2;
>> vreset = 0;
>> tau = 0.02
tau =
0.02
back
Howtos
Example (myscript.m)
1 %t h i s i s my f i r s t s c r i p t . I t d i s p l a y s a random number
2 random number = rand ( 1 ) ;
3 f p r i n t f ( ’A random number : %1.4 f : \ n ’ , random number ) ;
back
Howtos
a = rand ( 1 ) ;
i f a == 0 . 5
f p r i n t f ( ’ you a r e v e r y l u c k y ! ’ ) ;
end
back
Howtos
a =0;
for i = 1:100
a = a+i ;
end
back
Howtos
straightforward
however, carefully check
the size of matrices
if element-wise or matrix-like operations are intended
which matrix dimension to operate on
back
Howtos
Gamma distribution
back
Howtos
>> W = sparse(W);
In general, the syntax for using sparse matrices is the same as for
regular matrices.
Example (Sparse matrix)
>> W = double(rand(100,100)>0.9);
>> Ws = sparse(W);
>> y = Ws*x; % the same as y=W*x but faster
back
Howtos
Solution to Excercise #1
1 %compute t h e S t i r l i n g Formula
2
3 n = 50;
4
5 n f a c t o r i a l = s q r t ( 2 ∗ p i ∗n ) ∗ ( n/ exp ( 1 ) ) ˆ n
back to text
Howtos
Solution to Excercise #2
1 function n f a c t o r i a l = s t i r f a c (n ) ;
2 %compute t h e S t i r l i n g Formula
3
4 n f a c t o r i a l = s q r t ( 2 ∗ p i ∗n ) ∗ ( n/ exp ( 1 ) ) ˆ n ;
back to text