MATLAB Tutorial – CCN Course 2012 - · PDF fileIntroduction Arrays Coding Plotting...
Transcript of MATLAB Tutorial – CCN Course 2012 - · PDF fileIntroduction Arrays Coding Plotting...
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
MATLAB Tutorial – CCN Course 2012How to code a neural network simulation
Malte J. Rasch
National Key Laboratory of Cognitive Neuroscience and LearningBeijing Normal University
China
July 17, 2012
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Overview
Basic introduction to Matlab
Learn to code a neural network simulation
Further exercises with solutions
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Why MATLAB ?
Pro:
Matrix-like numerical operations very fast and easy to useGood plotting capabilitiesScript language for programming small to medium sizedproblems in applied mathematics (rapid prototyping)Widely used in the neuroscience community for data analysisas well as computational projects
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Why MATLAB ?
Pro:
Matrix-like numerical operations very fast and easy to useGood plotting capabilitiesScript language for programming small to medium sizedproblems in applied mathematics (rapid prototyping)Widely used in the neuroscience community for data analysisas 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 versatilespecialized software for detailed/large neural networksimulations(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 Solutions
Starting MATLAB
Easy: Just click on MATLAB symbol...
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Scalar expressions
Binary operations: work as expected, use = + - * / ^.
Example (compute y = a2x2+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
Example ( compute y =√
sin xln 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 Solutions
MATLAB == MATrix LABoratory
Not suprisingly, in MATLAB everything is about matrices.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
MATLAB == MATrix LABoratory
Not suprisingly, in MATLAB everything is about matrices.
However, the matrix-like datastructure in MATLAB is better calleda n-dimensional array, because it can be manipulated innon-algebraic ways.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
MATLAB == MATrix LABoratory
Not suprisingly, in MATLAB everything is about matrices.
However, the matrix-like datastructure in MATLAB is better calleda n-dimensional array, because it can be manipulated innon-algebraic ways.
(Almost) all functions will work on arrays as well
usually element-wise
Many MATLAB functions will produce arrays as output
Array operations much faster than for-looped element-wiseoperation
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Arrays: Overview
How to initialize arrays
Indexing
Calculating with arrays
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Calculating with arrays is
straight-forward
however, carefully check
the size of matricesif element-wise or matrix-like operations are intendedwhich matrix dimension to operate on
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Calculating with arrays is
straight-forward
however, carefully check
the size of matricesif element-wise or matrix-like operations are intendedwhich matrix dimension to operate on
Example (compute yi = Axi + b with b, xi ∈ R2)
>> A = [1,0.2;0.4,1];
>> b = [1;2] + 0.1;
>> x = 2*randn(2,1);
>> y = A * x + b
y =
4.5535
1.4856
>> N = 5;
% same as b = [b,b,b,b,b]
>> bi = repmat(b,[1,N]);
>> xi = 2*randn(2,N);
>> xi(:,1) = x;
>> yi = A * xi + bi
yi =
4.5535 2.3126 [..] -0.9021
1.4856 6.8091 [..] -0.1080
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Syntax for initializing arrays
1 Implicitly, using function returning an array
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Syntax for initializing arrays
1 Implicitly, using function returning an array
2 By explicit concatenation
Concatenation of columns of arrays
[ arr1, arr2, ... , arrn]
Concatenation of rows of arrays
[ arr1; arr2; ... ; arrn]
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Syntax for initializing arrays
1 Implicitly, using function returning an array
2 By explicit concatenation
Concatenation of columns of arrays
[ arr1, arr2, ... , arrn]
Concatenation of rows of arrays
[ arr1; arr2; ... ; arrn]
Note: an scalar is also regarded as an array (of size [1,1]).Note 2: arrays must have matching sizes.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Syntax for initializing arrays
1 Implicitly, using function returning an array2 By explicit concatenation
Concatenation of columns of arrays[ arr1, arr2, ... , arrn]
Concatenation of rows of arrays[ arr1; arr2; ... ; arrn]
Note: an scalar is also regarded as an array (of size [1,1]).Note 2: arrays must have matching sizes.
Example (Concatenation)
>> A = [1,2;3,4]
A =
1 2
3 4
>> B = [A,A]
B =
1 2 1 2
3 4 3 4
>> C = [A;A]
C =
1 2
3 4
1 2
3 4
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Syntax for initializing arrays: implicitly
Functions that pre-allocate memory and set each array element toan initial value:
zeros – all zero ND-arrays
ones – all one ND-arrays
rand – random ND-arrays (equally in [0, 1])
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Syntax for initializing arrays: implicitly
Functions that pre-allocate memory and set each array element toan initial value:
zeros – all zero ND-arrays
ones – all one ND-arrays
rand – random ND-arrays (equally in [0, 1])
colon (:) – for linear sequences, syntax:
istart:[step:]iend
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Syntax for initializing arrays: implicitly
Functions that pre-allocate memory and set each array element toan initial value:
zeros – all zero ND-arrays
ones – all one ND-arrays
rand – random ND-arrays (equally in [0, 1])
colon (:) – for linear sequences, syntax:
istart:[step:]iend
– many others, for details type: help command
randn, linspace, logspace, ndgrid, repmat,...
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Syntax for initializing arrays: implicitly
Functions that pre-allocate memory and set each array element toan initial value:
zeros – all zero ND-arraysones – all one ND-arraysrand – random ND-arrays (equally in [0, 1])
colon (:) – for linear sequences, syntax:
istart:[step:]iend
– many others, for details type: help command
randn, linspace, logspace, ndgrid, repmat,...
Example (Functions initializing arrays)
>> A = zeros(3,3);
>> A = ones(4,4,4);
>> size(A)
ans =
4 4 4
>> x = 3:-0.5:1
x =
3.0 2.5 2.0 1.5 1.0
>> A = ones(2)
1 1
1 1
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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)
Note: The first index is always 1 (not 0 as in most other languages)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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)
Note: The first index is always 1 (not 0 as in most other languages)
2 Linear index of a matrix:
access the (i , j)th element of the 2D-matrix A of dimension (m, n)
>> linearidx = i + (j-1)*m;
>> A(linearidx)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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)
Note: The first index is always 1 (not 0 as in most other languages)
2 Linear index of a matrix:
access the (i , j)th element of the 2D-matrix A of dimension (m, n)
>> linearidx = i + (j-1)*m;
>> A(linearidx)
3 “Slice” indexing with “:”
access the i-th row and jth column in A, respectively
>> A(i,:)
>> A(:,j)
get all elements as a cocatenated column vector
>> A(:)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Indexing arrays (2)
4 Multiple indices
vectors of linear indices can be used
>> A([1, 4, 5 ,6])
access the 1st to 4th rows of the 2D-matrix A of dimension (m, n)
>> A(1:4,:)
access the 2nd (m,n)-slice of a 3D-matrix B of dimension (m, n, p)
>> B(:,:,2)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Indexing arrays (2)
4 Multiple indices
vectors of linear indices can be used
>> A([1, 4, 5 ,6])
access the 1st to 4th rows of the 2D-matrix A of dimension (m, n)
>> A(1:4,:)
access the 2nd (m,n)-slice of a 3D-matrix B of dimension (m, n, p)
>> 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 Solutions
Calculating with arrays
1 Element-wise interpretationFor instance, sin cos log etc.Reserved symbols, .* ./ .^
2 “true” matrix interpretation (with dot product)Symbols * / ^ etc.
3 Operations on one specified dimensions of the matrixFor instance, sum mean max etc.
4 Array manipulationseg. reshape repmat permute circshift tril
Example (element-wise product and dot product)
>> A = ones(2,2);
>> A.*A
ans =
1 1
1 1
>> A*A
ans =
2 2
2 2
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Useful operations on specified dimensions
Often used functions on array elements include
size – number of elements in a dimension
sum – sum of elements along an array dimension
prod – product of elements
all, any – logical AND or OR, respectively
mean – mean of elements
max – maximal element of an array dimension
– and many more, eg. min var std median diff
Functions are usually called as
res = func(arr,dim);
Note: max and min are exceptions! Here: res = max(arr,[],dim);
For details type: help func
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
MATLAB is a script language
Scripts are blocks of code which can be called within MATLAB orwithin another script.
They are text-files with extensions “.m”.
They should contain all commands associated with a scientificproject.(at least to easily reproduce and the calculations)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
MATLAB is a script language
Scripts are blocks of code which can be called within MATLAB orwithin another script.
They are text-files with extensions “.m”.
They should contain all commands associated with a scientificproject.(at least to easily reproduce and the calculations)
There are basically two types of m-files
1 m-file script
A squential list of MATLAB commandsThe variable scope is that of the caller.
2 m-file function
m-file functions accept input parameters and deliver outputs.The variable scope is different from that of the caller.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file scripts
How to write an m-file script?
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file scripts
How to write an m-file script?
1 open a text-editor of your choice or use the editor providedwith MATLAB
>> edit myscript
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file scripts
How to write an m-file script?
1 open a text-editor of your choice or use the editor providedwith MATLAB
>> edit myscript
2 Write all your calculations in a text-file with extension “.m”(here myscript.m)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file scripts
How to write an m-file script?
1 open a text-editor of your choice or use the editor providedwith MATLAB
>> edit myscript
2 Write all your calculations in a text-file with extension “.m”(here myscript.m)
3 Save file in the current working directory(or addpath to search path )
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file scripts
How to write an m-file script?
1 open a text-editor of your choice or use the editor providedwith MATLAB
>> edit myscript
2 Write all your calculations in a text-file with extension “.m”(here myscript.m)
3 Save file in the current working directory(or addpath to search path )
4 Call your script by calling it from the “Command Window”
>> myscript;
(no compilation needed)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file scripts
How to write an m-file script?
1 open a text-editor of your choice or use the editor providedwith MATLAB
>> edit myscript
2 Write all your calculations in a text-file with extension “.m”(here myscript.m)
3 Save file in the current working directory(or addpath to search path )
4 Call your script by calling it from the “Command Window”
>> myscript;
(no compilation needed)
5 Note: The variable scope is that of the caller. This means thatthe variables of the script are now present in the workspace
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file scripts
Excercise #1 (How to write an m-file script)
Write an m-file computing the Stirling approximation
n! ≈√2πn
(n
e
)n
Hint: π is defined in MATLAB as pi, and ex as exp(x).Solution #1
Reminder:
1 Open a text-editor
>> edit myscript
2 Write calculations in text-file with extension “.m”
3 Save file into current working directory
4 Call your script
>> myscript;
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file functions
How to write an m-file function?
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file functions
How to write an m-file function?
Identical to writing an m-file script, except:
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file functions
How to write an m-file function?
Identical to writing an m-file script, except:
In the first line input and output arguments are defined:
function outarg = myfun(inarg1,inarg2,...);
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file functions
How to write an m-file function?
Identical to writing an m-file script, except:
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 Solutions
m-file functions
How to write an m-file function?
Identical to writing an m-file script, except:
In the first line input and output arguments are defined:
function outarg = myfun(inarg1,inarg2,...);
Call the function with
>> result = myfun(p1,p2,...);
Note: The variable scope is different from that of the caller. Thatmeans, 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 Solutions
m-file functions
How to write an m-file function?
Identical to writing an m-file script, except:
In the first line input and output arguments are defined:
function outarg = myfun(inarg1,inarg2,...);
Call the function with
>> result = myfun(p1,p2,...);
Note: The variable scope is different from that of the caller. Thatmeans, that variables defined in the function body are NOT
present in the workspace after calling the functionNote 2: Input arguments are always referenced by value
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
m-file functions
How to write an m-file function?
Identical to writing an m-file script, except:
In the first line input and output arguments are defined:
function outarg = myfun(inarg1,inarg2,...);
Call the function with
>> result = myfun(p1,p2,...);
Note: The variable scope is different from that of the caller. Thatmeans, that variables defined in the function body are NOT
present in the workspace after calling the functionNote 2: Input arguments are always referenced by valueNote 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 Solutions
m-file functions
Excercise #2 (writing a m-file function)
Write an m-file function computing the Stirling approximation
n! ≈√2πn
(n
e
)n
for a given nSolution #2
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 Solutions
Basic syntax
The basic syntax is similar to other script languages, eg. “python”.
MATLAB has the usual flow control structures, namely
if .. else for decisions
for-loop
while-loop
switch .. case for multiple if-else decisions
try .. catch for handling errors
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Basic syntax: flow control (1)
if-else block syntax:
1 i f s c a l a r c o n d i t i o n2 e x p r e s s i o n s3 e l s e
4 e x p r e s s i o n s5 end
Relational operators, eg.: == (equals), || (or), && (and), ~ (not)
for details type: help relop
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Basic syntax: flow control (1)
if-else block syntax:
1 i f s c a l a r c o n d i t i o n2 e x p r e s s i o n s3 e l s e
4 e x p r e s s i o n s5 end
Relational operators, eg.: == (equals), || (or), && (and), ~ (not)
for details type: help relop
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 Solutions
Programming: basic flow control (2)
for-loop block syntax:
1 f o r i = a r r a y2 % i==a r r a y ( j ) i n the j−th l oop3 e x p r e s s i o n s4 end
(one can also use break and continue keywords)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Programming: basic flow control (2)
for-loop block syntax:
1 f o r i = a r r a y2 % i==a r r a y ( j ) i n the j−th l oop3 e x p r e s s i o n s4 end
(one can also use break and continue keywords)
Example (for loop)
a=0;f o r i = 1 :100
a = a+i ;end
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Plotting with MATLAB
Very flexible plotting tools
Many functions for a variety of plot types and graphicsdrawing
A
B C D EStatic
Res
pons
e
0° 90° 180°
High speed
→ M
ovem
ent d
ir. →
0° 90° 180°
Low speed
→ M
ovem
ent d
ir. →
0° 90° 180°
−20
2
−202−10
−5
0
5
10
SF [cyc/deg]
High speed
SF [cyc/deg]
Low speed
TF
[Hz]
Horz. RF
Vert. RF
t
Stimulus
x
y
RF filter Kθ,ρ,ω
vary θ t
Simple cell response
x
y
⟨ |⋅|2 ⟩x,y,t
(+ phase invariance)
θ2
θ3
:
0
0.5
1
1.5
2Response profile
Avg
. res
pons
e
0° 90° 180°
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Overview
Just two most useful plotting commands
plot – plots all kinds of lines, dots, markers in 2D
imagesc – plots an 2-D pseudo image
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Overview
Just two most useful plotting commands
plot – plots all kinds of lines, dots, markers in 2D
imagesc – plots an 2-D pseudo image
Note: For an overview of 2D plotting commands: help graph2d
Note 2: For fancy graphs see: help specgraph
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 Solutions
plot command
Basic syntax:
handle = plot(X,Y,linespec,optname1,val1,...);
X,Y – x- and y-values to plot. If Y is a 2-D array, all columns areplotted as different lines
linespec – a string with a short hand for color and line style andmarker type. See help plot for an overview. Eg,
linespec = ’:ko’
plots dotted (:) black line (k) with a circle at each givencoordinate (xi , yi )
optname1 – a string specifing the option name, eg. ’LineWidth’
val1 – a corresponding value.
handle – graphics handle for later reference, eg. with
>> set(handle,optname1,val1)
Tip: To get an overview over all possible options, try get(handle)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Plotting example: empirical PDF of Gaussian
1 f i g u r e ;2
3 % p l o t Gaus s i an PDF4 x = −4 : 0 . 01 : 4 ;5 y = exp (−0.5∗ x . ˆ2 ) / s q r t (2∗ p i ) ;6 p l o t ( x , y , ’−k ’ , ’ L i n ew id th ’ , 2 ) ;7
8 % emp i r i c a l pd f9 % hi s tog ram o f 1000 random v a l u e s
10 [N, z ]= h i s t ( randn ( 1 0 00 , 1 ) ) ;11 %no rma l i z a t i o n12 p = N/sum (N)/( z (2)− z ( 1 ) ) ;13
14 ho ld on ; %adds a l l f o l l o w i n g graphs15 %to the c u r r e n t axe s16 p l o t ( z , p , ’ r x ’ , ’ Marke rS i ze ’ , 1 0 ) ;17 ho ld o f f ;18
19 x l a b e l ( ’Random v a r i a b l e x ’ )20 y l a b e l ( ’ P r o b a b i l i t y d e n s i t y ’ )21 l egend ( ’ t r u e PDF ’ , ’ em p i r i c a l PDF ’ )
−4 −3 −2 −1 0 1 2 3 40
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
Random variable x
Pro
babi
lity
dens
ity
true PDFempirical PDF
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Goal of tutorial
We will program a neural network simulation together.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
We will practice on the way:
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 Solutions
Network model simulation
What has to be done in principle:
n neurons, excitatory and inhibitory, are inter-connected withsynapses.
The each neuron and synapse follows a particular dynamicover time.
The simulation calculates the dynamic and spiking activity ofeach neuron for each t.
The network gets some input and reacts in a certain and theresponse of the network is plotted.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
We will proceed in 4 successive steps
1 Simulate a single neuron with current step input2 Simulate a single neuron with Poisson input3 Simulate 1000 neurons (no recurrent connections)4 Simulate complete network (conductance based)
Network activity
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Which neuron model to use?
Biophysical model (i.e. Hodgkin-Huxley model)
Cm
dVm
dt= − 1
Rm
(Vm − VL)−∑
i
gi (t)(Vm − Ei ) + Isyn + Iapp
Including non-linear dynamics of many channels in gi (t)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Which neuron model to use?
Biophysical model (i.e. Hodgkin-Huxley model)
Cm
dVm
dt= − 1
Rm
(Vm − VL)−∑
i
gi (t)(Vm − Ei ) + Isyn + Iapp
Including non-linear dynamics of many channels in gi (t)
Mathematical simplification (Izhikevich, book chapter 8)
v = (0.04v + 5) v + 150− u −∑
j
wjgj(v − Ej) + Iapp
u = a (b v − u)
v ← c , u ← u + d , if v ≥ 35
with gj = −gj/τg and gj ← gj + 1 if vj ≥ 35. It is b = 0.2, c = −65, andd = 8, a = 0.02 for exc. neurons and d = 2, a = 0.1 for inh. neurons.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Neuron model
regular spiking (RS) intrinsically bursting (IB) chattering (CH) fast spiking (FS)
40 ms
20 mV
low-threshold spiking (LTS)
para
met
er b
parameter c
para
met
er d
thalamo-cortical (TC)
-87 mV
-63 mV
thalamo-cortical (TC)
peak 30 mV
reset c
reset ddecay with rate a
sensitivity b
v(t)
u(t)0 0.1
0.05
0.2
0.25
RS,IB,CH FS
LTS,TC
-65 -55 -50
2
4
8
IB
CH
RS
FS,LTS,RZ
TC
0.02parameter a
resonator (RZ)
RZ
v(t)
I(t)
v'= 0.04v 2+5v +140 - u + I u'= a(bv - u) if v = 30 mV, then v c, u u + d
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Step 1: Simulate a single neuron with injected current
Exercise 1
Simulate the neuron model for 1000ms and plot the resultingvoltage trace. Apply a current step (Iapp = 7pA) between time200ms and 700ms.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Step 1: Simulate a single neuron with injected current
Exercise 1
Simulate the neuron model for 1000ms and plot the resultingvoltage trace. Apply a current step (Iapp = 7pA) between time200ms 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
with d = 8, a = 0.02, b = 0.2,
c = −65 for an excitatory neuron.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 Solutions
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
2 Reserve memory for voltage trace v and u (of length T = 1000/∆t)HowTo and set first element to −70 and −14, respectively.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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
2 Reserve memory for voltage trace v and u (of length T = 1000/∆t)HowTo and set first element to −70 and −14, respectively.
3 Loop over T − 1 time steps and do for each step t HowTo
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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
2 Reserve memory for voltage trace v and u (of length T = 1000/∆t)HowTo and set first element to −70 and −14, respectively.
3 Loop over T − 1 time steps and do for each step t HowTo
1 set Iapp ← 7 if t∆t is between 200 and 700 (otherwise 0)HowTo
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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
2 Reserve memory for voltage trace v and u (of length T = 1000/∆t)HowTo and set first element to −70 and −14, respectively.
3 Loop over T − 1 time steps and do for each step t HowTo
1 set Iapp ← 7 if t∆t is between 200 and 700 (otherwise 0)HowTo
2 if vt < 35: update element t + 1 HowTo of v and u
according to HowTo
vt+1 ← vt +∆t {(0.04 vt + 5) vt − ut + 140 + Iapp}ut+1 ← ut +∆t a (b vt − ut)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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
2 Reserve memory for voltage trace v and u (of length T = 1000/∆t)HowTo and set first element to −70 and −14, respectively.
3 Loop over T − 1 time steps and do for each step t HowTo
1 set Iapp ← 7 if t∆t is between 200 and 700 (otherwise 0)HowTo
2 if vt < 35: update element t + 1 HowTo of v and u
according to HowTo
vt+1 ← vt +∆t {(0.04 vt + 5) vt − ut + 140 + Iapp}ut+1 ← ut +∆t a (b vt − ut)
3 if vt ≥ 35: set vt+1 ← c and ut+1 ← ut + d (and optionalvt ← 35)
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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
2 Reserve memory for voltage trace v and u (of length T = 1000/∆t)HowTo and set first element to −70 and −14, respectively.
3 Loop over T − 1 time steps and do for each step t HowTo
1 set Iapp ← 7 if t∆t is between 200 and 700 (otherwise 0)HowTo
2 if vt < 35: update element t + 1 HowTo of v and u
according to HowTo
vt+1 ← vt +∆t {(0.04 vt + 5) vt − ut + 140 + Iapp}ut+1 ← ut +∆t a (b vt − ut)
3 if vt ≥ 35: set vt+1 ← c and ut+1 ← ut + d (and optionalvt ← 35)
4 Plot the voltage trace v versus t HowTo
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Solution to step 1
1 % 1) i n i t i a l i z e pa ramete r s2 dt = 0 . 5 ;3 d = 8 ;4 a = 0 . 0 2 ;5 c = −65;6 b = 0 . 2 ;7 T = c e i l (1000/ dt ) ;89 % 2) r e s e r v e memory
10 v = ze ro s (T , 1 ) ;11 u = ze ro s (T , 1 ) ;12 v (1 ) = −70; % r e s t i n g p o t e n t i a l13 u (1 ) = −14; % s t eady s t a t e1415 %3) fo r−l oop ove r t ime16 f o r t = 1 :T−1;17 %3 . 1 ) ge t i n pu t18 i f t∗dt>200 && t∗dt<70019 Iapp = 7 ;20 e l s e
21 Iapp = 0 ;
22 end
2324 i f v ( t )<3525 %3 . 2 ) update ODE26 dv = (0 .04∗ v ( t )+5)∗v ( t )+140−u ( t ) ;27 v ( t+1) = v ( t ) + ( dv+Iapp )∗ dt ;28 du = a∗(b∗v ( t)−u ( t ) ) ;29 u ( t+1) = u ( t ) + dt∗du ;30 e l s e
31 %3 . 3 ) s p i k e !32 v ( t ) = 35 ;33 v ( t+1) = c ;34 u ( t+1) = u ( t )+d ;35 end
36 end
3738 % 4) p l o t v o l t a g e t r a c e39 p l o t ( ( 0 :T−1)∗dt , v , ’ b ’ ) ;40 x l a b e l ( ’ Time [ms ] ’ ) ;41 y l a b e l ( ’Membrane v o l t a g e [mV] ’ ) ;
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Step 2: Single neuron with synaptic input
Exercise 2
Simulate the neuron model for 1000ms and plot the resultingvoltage trace. Assume that 100 synapses are attached to theneuron, with each pre-synaptic neuron firing with a Poissonprocess 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 Solutions
Step 2: Single neuron with synaptic input
Exercise 2
Simulate the neuron model for 1000ms and plot the resultingvoltage trace. Assume that 100 synapses are attached to theneuron, with each pre-synaptic neuron firing with a Poissonprocess of rate frate = 2 Hz between time 200ms and 700ms.
Synaptic input model:
Isyn =∑
j
winj g
inj (t)(E
inj − v(t))
ginj = g
inj /τg
ginj ← g
inj + 1, if rj(t) < frate∆t
with τg = 10ms, weights w inj = 0.07,
Ej = 0, rj(t) ∈ [0, 1] uniform
random numbers drawn for each
step t, and j = 1 . . . 100.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 Solutions
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 = (g inj ), win = (w in
j ), andE = (Ej) (vectors of length nin = 100) with constant elements
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 = (g inj ), win = (w in
j ), andE = (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 Solutions
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 = (g inj ), win = (w in
j ), andE = (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 isbetween 200 and 700 (otherwise 0). r = (rj) is a vector ofuniform random numbers of length nin
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 = (g inj ), win = (w in
j ), andE = (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 isbetween 200 and 700 (otherwise 0). r = (rj) is a vector ofuniform random numbers of length nin
2 before the vt update: implement the conductance dynamics gand set Iapp (using array notation HowTo carefully HowTo ):
g inj ← g in
j + pj
Iapp ← win ·(
gin ⊙ Ein)
−(
win · gin)
⊙ vt
g inj ← (1−∆t/τg ) g
inj
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Solution to step 2
1 %i n i t i a l i z e pa ramete r s2 dt = 0 . 5 ;3 d = 8 ;4 a = 0 . 0 2 ;5 c = −65;6 b = 0 .278 %r e s e r v e memory9 T = c e i l (1000/ dt ) ;
10 v = ze ro s (T , 1 ) ;11 u = ze ro s (T , 1 ) ;12 v (1 ) = −70;13 u (1 ) = −14;1415 n i n = 100 ; %NEW16 r a t e = 2∗1e−3;%to [ms ] %NEW17 tau g = 10 ; %NEW18 g i n = ze ro s ( n in , 1 ) ; %NEW19 E in = ze ro s ( n in , 1 ) ; %NEW20 w in = 0.07∗ ones (1 , n i n ) ; %NEW2122 %fo r−l oop ove r t ime23 f o r t = 1 :T−1;24 %Po i s son i npu t25 i f t∗dt>200 && t∗dt<70026 p = rand ( n in ,1)< r a t e∗dt ; %NEW27 e l s e
28 p = 0 ; %NEW29 end
30 %conductance update o f g i n31 g i n = g i n + p ;32 Iapp = w in ∗( g i n .∗ E in ) ;33 Iapp = Iapp −(w in∗ g i n ) .∗ v ( t ) ;34 g i n = (1 − dt / tau g )∗ g i n ;3536 i f v ( t )<3537 %update ODE38 dv = (0 .04∗ v ( t )+5)∗v ( t )+140−u ( t ) ;39 v ( t+1) = v ( t ) + ( dv+Iapp )∗ dt ;40 du = a∗(b∗v ( t)−u ( t ) ) ;41 u ( t+1) = u ( t ) + dt∗du ;42 e l s e
43 %s p i k e s44 v ( t ) = 35 ;45 v ( t+1) = c ;46 u ( t+1) = u ( t )+d ;47 end
4849 end
5051 %p l o t v o l t a g e t r a c e52 p l o t ( ( 0 :T−1)∗dt , v , ’ b ’ ) ;53 x l a b e l ( ’ Time [ms ] ’ ) ;54 y l a b e l ( ’Membrane v o l t a g e [mV] ’ ) ;
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Step 3: Simulate 1000 neurons (not inter-connected)
Exercise 3
Simulate 1000 neurons for 1000 ms and plot the resulting spikes.Assume that each neuron receives (random) 10% of the 100Poisson spike trains of rate frate = 2 Hz between time 200 ms and700 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 Solutions
Step 3: Simulate 1000 neurons (not inter-connected)
Exercise 3
Simulate 1000 neurons for 1000 ms and plot the resulting spikes.Assume that each neuron receives (random) 10% of the 100Poisson spike trains of rate frate = 2 Hz between time 200 ms and700 ms. Note that the neurons are not yet inter-connected.
Excitatory and inhibitory neurons:
A neuron is, with probability pI = 0.2,
a (fast-spiking) inhibitory neuron
(a = 0.1, d = 2), others are (regular
spiking) excitatory neurons (a = 0.02
and d = 8). Weights of the input
synapse j to inhibitory neuron i is
w in = 0.07 if connected (otherwise 0).
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 Solutions
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 oflength 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 Solutions
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 oflength 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 Solutions
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 oflength 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 w inij = 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 Solutions
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 oflength 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 w inij = 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 arraynotation to update all i neurons simultaneously.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 oflength 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 w inij = 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 arraynotation to update all i neurons simultaneously.
6 Plot the spike raster. Plot black dots at {(t, i)|vit ≥ 35} forexcitatory neuron i . Use red dots for inhibitory neurons.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Solution to step 31 % i n i t i a l i z e pa ramete r s2 dt = 0 . 5 ;3 n = 1000 ; % no o f neurons4 i nh = rand (n ,1)<0 .2 ; % k inh5 exc = ˜ inh ; % o t h e r s exc6 d = 8∗ exc + 2∗ i nh ; % f o r exc / i nh7 a = 0.02∗ exc + 0.1∗ i nh ;89 % r e s e r v e memory
10 T = c e i l (1000/ dt ) ;11 v = ze ro s (n ,T) ; % now : n x T12 u = ze ro s (n ,T) ; % now : n x T13 v ( : , 1 ) = −70; % v e c t o r s14 u ( : , 1 ) = −14;1516 n i n = 100 ;17 r a t e = 2∗1e−3;18 tau g = 10 ;19 g i n = ze ro s ( n in , 1 ) ;20 E i n = ze ro s ( n in , 1 ) ;21 W in = 0.07∗ ones (n , n i n ) ; % n x n i n22 W in ( rand (n , n i n )>0.1) = 0 ;%conn . prob2324 % fo r−l oop ove r t ime25 f o r t = 1 :T−1;26 i f t∗dt>200 && t∗dt<70027 p = rand ( n in ,1)< r a t e∗dt ;28 e l s e
29 p = 0 ;30 end
31 g i n = g i n + p ;
32 Iapp = w in ∗( g i n .∗ E in ) ;33 Iapp = Iapp −(W in∗ g i n ) .∗ v ( : , t ) ; %NEW34 g i n = (1 − dt / tau g )∗ g i n ;3536 %update v e c t o r i z e d ODE37 dv =(0.04∗ v ( : , t )+5).∗ v ( : , t )+140−u ( : , t ) ;38 v ( : , t+1) = v ( : , t ) + ( dv+Iapp )∗ dt ;39 du = a .∗ (0 . 2∗ v ( : , t)−u ( : , t ) ) ;40 u ( : , t+1) = u ( : , t ) + dt∗du ;4142 %hand l e s p i k e s ( r e s e t v , u )43 f i r e d = v ( : , t )>=35; % neurons f i r e d ?44 v ( f i r e d , t ) = 35 ;45 v ( f i r e d , t+1) = −65;46 u ( f i r e d , t+1) = u ( f i r e d , t )+d ( f i r e d ) ;47 end
4849 %p l o t s p i k e r a s t e r50 spks = doub l e ( v==35);51 spks ( inh , : ) = 2∗ spks ( inh , : ) ;52 c l f , ho ld on ;53 [X,Y] = meshgr id ( ( 0 :T−1)∗dt , 1 : n ) ;54 c o l = ’ k r ’ ;55 f o r k = 1 :2 % exc / i nh56 i d x = f i n d ( spks==k ) ;57 p l o t (X( i d x ) ,Y( i d x ) , [ c o l ( k ) ’ . ’ ] ) ;58 end
59 x l im ( [ 0 ,T∗dt ] ) ; y l im ( [ 0 , n ] )60 x l a b e l ( ’ Time [ms ] ’ )61 y l a b e l ( ’ Un i t #’ )
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Step 4: Simulate recurrent network
Exercise 4
Simulate 1000 neurons as before but with added recurrentconnections.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Step 4: Simulate recurrent network
Exercise 4
Simulate 1000 neurons as before but with added recurrentconnections.
Recurrent synaptic activations
A neuron i is sparsely (with probabilityprc = 0.1) connected to a neuron j .Thus neuron i receives an additionalcurrent I syni of the form:
Isyni =
n∑
j=1
wijgj(t) (Ej − vi (t))
Weights are Gamma distributed (scale
0.003, shape 2). Inh. to exc.
connections are twice as strong.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 Solutions
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 Solutions
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 aGamma distribution of scale 0.003 and shape 2 HowTo
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 aGamma 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 Solutions
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 aGamma distribution of scale 0.003 and shape 2 HowTo
4 Make the weight matrix “sparse” HowTo
5 Scale weights from inh. to exc. neurons by the factor of 2
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
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 aGamma distribution of scale 0.003 and shape 2 HowTo
4 Make the weight matrix “sparse” HowTo
5 Scale weights from inh. to exc. neurons by the factor of 2
6 Inside the for-loop change/add the following:
1 add the equations for recurrent conductances gj
gj ← gj + 1, if vj(t − 1) ≥ 35
Isyn ← 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 Solutions
Solution to step 41 % i n i t i a l i z e pa ramete r s2 dt = 0 . 5 ;3 n = 1000 ;4 i nh = rand (n ,1)<0 .2 ;5 exc = ˜ inh ;6 d = 8∗ exc + 2∗ i nh ;7 a = 0.02∗ exc + 0.1∗ i nh ;89 % r e s e r v e memory
10 T = c e i l (1000/ dt ) ;11 v = ze ro s (n ,T) ;12 u = ze ro s (n ,T) ;13 v ( : , 1 ) = −70;14 u ( : , 1 ) = −14;1516 n i n = 100 ;17 r a t e = 2∗1e−3;18 tau g = 10 ;19 g i n = ze ro s ( n in , 1 ) ;20 E i n = ze ro s ( n in , 1 ) ;21 W in = 0.07∗ ones (n , n i n ) ;22 W in ( rand (n , n i n )>0.1) = 0 ;2324 g = ze ro s (n , 1 ) ; % NEW: i n i t RC25 E = ze ro s (n , 1 ) ;26 E( i nh ) = −85;27 W = ze ro s (n , n ) ; % i n i t W with 028 i d x = f i n d ( rand (n , n )<0.1) ;%choose e l e .29 W( i d x ) = gamrnd ( 2 , . 0 0 3 , l eng th ( i d x ) , 1 ) ;30 W( exc , i nh ) = 2∗W( exc , i nh ) ;%s c a l e I−>E31 W = spa r s e (W) ; %f a s t e r . .32 f i r e d = 0 ;33
34 % fo r−l oop ove r t ime35 f o r t = 1 :T−1;36 %Po i s son i npu t37 i f t∗dt>200 && t∗dt<70038 p = rand ( n in ,1)< r a t e∗dt ;39 e l s e
40 p = 0 ;41 end
42 % inpu t conduc tance s43 g i n = g i n + p ;44 Iapp = W in∗( g i n .∗ E in ) ;45 Iapp = Iapp −(W in∗ g i n ) .∗ v ( : , t ) ;46 g i n = (1 − dt / tau g )∗ g i n ;4748 %NEW r e c u r r e n t conduc tance s49 g = g + f i r e d ; % s p i k e s have a r r i v e d50 I s y n = W∗(g .∗E) − (W∗g ) .∗ v ( : , t ) ;51 Iapp = Iapp + I s y n ;52 g = (1 − dt / tau g )∗g ;5354 %update v e c t o r i z e d ODE55 dv =(0.04∗ v ( : , t )+5).∗ v ( : , t )+140−u ( : , t ) ;56 v ( : , t+1) = v ( : , t ) + ( dv+Iapp )∗ dt ;57 du = a .∗ (0 . 2∗ v ( : , t)−u ( : , t ) ) ;58 u ( : , t+1) = u ( : , t ) + dt∗du ;5960 %hand l e s p i k e s ( r e s e t v , u )61 f i r e d = v ( : , t )>=35; % neurons f i r e d ?62 v ( f i r e d , t ) = 35 ;63 v ( f i r e d , t+1) = −65;64 u ( f i r e d , t+1) = u ( f i r e d , t )+d ( f i r e d ) ;65 end
66 %p l o t s p i k e r a s t e r : as b e f o r e
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Congratulation !
You have just coded and simulated a
quite realistic network model !
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Optional exercises
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Exercises
Exercise (p-series)
Calculate the p-series (generalization of the Harmonic Series) for agiven p up to a given m
µm =
m∑
i=1
1
np
Use array notation. Solution
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 Solutions
Exercises
Exercise (blob movie)
1 Generate a two arrays with, x and y , ranging from -2 to 2(and about 100 elements)
2 Generate two 100 by 100 grid-matrices, X and Y usingmeshgrid with x and y as input (look at X and Y tounderstand what meshgrid is doing).
3 calculate a matrix Z of the same size as X and Y where eachelement is given by zi = e−(x2
i+y2
i).
4 write a loop with t ranging from 0 to 1000 where you
1 plot the matrix Z (using imagesc)2 circular shift the matrix Z by 1 element (using circshift)3 force the plot to be drawn (using drawnow)
Solution
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Exercises
Exercise (Logical indexing and basic plotting)
1 Generate a 100 by 2 matrix M of Gaussian random values
2 Plot a point cloud (first vs. second column of M) using bluecircles
3 Calculate how many values of M are positive
4 Erase all rows in M which have at least one negative value
5 Plot the points of the modified array M using red crosses inthe same graph as above.
6 Set both axes limits to −3 to 3 and plot dotted gray lines onthe coordinate axes (where y = 0 or x = 0).
7 Label the axes and write a legend
Solution
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Exercises
Exercise (Poisson spike trains)
Write a function that generates a homogeneous Poisson spike trainof rate λ having exactly N spikes. Use array notations (cumsum).
Hint: Poisson spike intervals t are exponentially distributed. Theycan be generated by inverse transform sampling: Given uniformrandom variables u in 0 to 1 valid inter-spike intervals can becalculated as
t = − log u
λSolution
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 Solutions
Exercises
Exercise (More on spike trains)
Generate a long Poisson spike train (with the function from thelast exercise). Compute the mean and standard deviation of theinter-spike interval distribution.
Further, write a function that counts the number of spike timesfalling into time-bins of length ∆t.
Hint: Use diff to get the intervals from spike times.
Hint 2: Use histc to bin the spike-timesSolution
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Exercises
Exercise (Plot 2-D Gaussian point cloud)
Write a function which plots a point cloud of n randomsamples drawn from a 2D Normal distribution (with 0mean) and variance Σ = (RD)T (RD), where therotation matrix is defined as
R =
(
cos θ − sin θsin θ cos θ
)
and D is a diagonal matrix of the standard deviation inthe principal directions
D =
(
σ1 00 σ2
)
The function should accept parameters σ1, σ2, and θ.
-5 0 5
-5
0
5
Hint: use randn to produce independent Normal-distributed random vectors xiand transform them according to yi = RDxi .
Solution
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Advanced exercise
Exercise (Optimizing Matlab code)
Optimize “bad” Matlab code for generating self-organizing maps.
See provided code and description in som_exercise.zip.
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Solution: P-series
1 f unc t i on mu = p s e r i e s (n , p ) ;2 % PSERIES (N,P) computes the P−s e r i e s up to N3
4 i a r r = ( 1 : n ) . ˆ p ;5 mu = sum ( 1 . / i a r r , 2 ) ;
back to text
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Solution: Blob movie1 x = l i n s pa c e (−2 ,2 ,100) ;2 y = l i n s pa c e (−2 ,2 ,100) ;3
4 [X ,Y] = meshgrid ( x , y ) ;5
6 Z = exp(−X.ˆ2 − Y . ˆ 2 ) ;7
8 f o r t = 1:10009 imagesc (Z ) ;
10 Z = c i r c s h i f t (Z , [ 0 , 1 ] ) ;11 drawnow ;12 end
back to text
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Solution: Logical indexing and basic plotting1 %1 .2 M = randn ( 1 0 0 , 2 ) ;34 %2 .5 p l o t (M( : , 1 ) ,M( : , 2 ) , ’ bo ’ ) ;67 %3 .8 npos = sum (M(:)>0)9
10 %4 .11 M( any (M<0 ,2) , : ) = [ ] ;1213 %5 .14 ho ld on ;15 p l o t (M( : , 1 ) ,M( : , 2 ) , ’ r x ’ ) ;1617 %6 .18 l i m i t s = [−3 ,3 ] ;19 x l im ( l i m i t s ) ; y l im ( l i m i t s )20 p l o t ( l i m i t s , [ 0 , 0 ] , ’ : ’ , ’ Co l o r ’ , [ 0 . 5 , 0 . 5 , 0 . 5 ] )21 p l o t ( [ 0 , 0 ] , l i m i t s , ’ : ’ , ’ Co l o r ’ , [ 0 . 5 , 0 . 5 , 0 . 5 ] )2223 %7 .24 x l a b e l ( ’X ’ )25 y l a b e l ( ’Y ’ )26 l egend ({ ’ Gaus s i an random samples ’ , ’ Po i n t s w i th X>0 and Y>0 ’})
back to text
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Solution: Poisson spike trains
1 f unc t i on spk t = p o i s s o n s p i k e s ( lambda ,N)2 % SPKT = POISSONSPIKES(LAMBDA,N) g e n e r a t e s a3 % Po i s son s p i k e t r a i n w i th r a t e LAMBDA4 % and e x a c t l y N s p i k e s .5
6 i s i s = −l og ( rand (N, 1 ) ) / lambda ;7 spk t = cumsum( i s i s ) ;
back to text
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Solution: More on spike trains1 N = 1000 ; % number o f s p i k e s2 lambda = 10 ; %s p i k e r a t e3
4 %Po i s son s p i k e s5 spk t = cumsum(− l og ( rand (N, 1 ) ) / lambda ) ;6
7 %mean o f the I S I8 mi s i = mean( d i f f ( spk t ) ) ;9
10 %stddev o f the I S I11 s i s i = std ( d i f f ( spk t ) ) ;12
13
14 f unc t i on S = s p i k e b i n n i n g ( spkt , twin , dt )15 % S = SPIKEBINNING(SPKT,TWIN,DT) count s the s p i k e t imes16 % oc c u r r i n g i n each b in o f width DT i n the t ime window17 % from TWIN(1) and TWIN(2)18
19 S = h i s t c ( spkt , tw in ( 1 ) : dt : tw in ( 2 ) ) ;
back to text
Introduction Arrays Coding Plotting Network model: Step 1 Step 2 Step 3 Step 4 Exercises Solutions
Solution: Plot 2-D Gaussian point cloud1 f u n c t i o n p lo t2DGaus s i an ( s i g1 , s i g2 , t h e t a ) ;2 % PLOT2DGAUSSIAN( SIG1 , SIG2 ,THETA) p l o t s a Gaus s i an po i n t c l oud wi th3 % s tanda rd d e v i a t i o n s SIG1 and SIG2 and o r i e n t a t i o n THETA.45 n = 250 ;6 r = randn (2 , n ) ;7 D = [ s i g1 , 0 ; 0 , s i g 2 ] ;8 R = [ cos ( t h e t a ),− s i n ( t h e t a ) ; s i n ( t h e t a ) , cos ( t h e t a ) ] ;9
10 x = R∗D∗ r ;1112 %p l o t t i n g13 f i g u r e ; subp l o t ( 2 , 2 , 1 ) ;14 p l o t ( x ( 1 , : ) , x ( 2 , : ) , ’ . b ’ , ’ Marke rS i ze ’ , 6 ) ;1516 %r e s i z e the axe s17 mxlen = 1.1∗max( abs ( x ( : ) ) ) ;18 x l im ([−mxlen , mxlen ] )19 y l im ([−mxlen , mxlen ] )2021 %p l o t the 0−c o o r d i n a t e s22 ho ld on ;23 p l o t ([−mxlen , mxlen ] , [ 0 , 0 ] , ’−−k ’ )24 p l o t ( [ 0 , 0 ] , [ −mxlen , mxlen ] , ’−−k ’ )25 ho ld o f f ;
back to text
Howtos
How to start Matlab
Easy: Just click on Matlab symbol...
back
Howtos
How to get help
Each Matlab function hasa header which describes itsusage
Just type:
>> help command
or
>> doc command
Alternatively:http://www.mathworks.com/access/helpdesk/help/techdoc/
back
Howtos
How to plot in general
How to plot:
1 Open a figure (window) with
>> figure;
2 Issue plotting command of your choice
>> plot(x,y,’b--’,’LineWidth’,2);
HowTo Note: graphics commands draw into the current axes (gca) on the
current figure (gcf). They automatically create a new figure and axes if
necessary.
3 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’)
back
Howtos
How to use the plot command
Basic syntax:
handle = plot(X,Y,linespec,optname1,val1,...);
X,Y – x- and y-values to plot. If Y is a 2-D array, all columns areplotted as different lines
linespec – a string with a short hand for color and line style andmarker type. See help plot for an overview. Eg,
linespec = ’:ko’
plots dotted (:) black line (k) with a circle at each givencoordinate (xi , yi )
optname1 – a string specifing the option name, eg. ’LineWidth’
val1 – a corresponding value.
handle – graphics handle for later reference, eg. with
>> set(handle,optname1,val1)
Tip: To get an overview over all possible options, try get(handle)back
Howtos
How to initialize parameters
Just use the syntax
>> parname = value;
Example
>> a = 2;
>> vreset = 0;
>> tau = 0.02
tau =
0.02
back
Howtos
How to use scalar expressions
Binary operations: work as expected, use = + - * / ^
Example (compute y = a2x2+a
+ b)
>> a = 2;
>> b = 1;
>> x = 0.5;
>> y = a^2*x/(2+a) + b;
>> y
y =
1.500
back
Howtos
How to initialize arrays
1 Implicitly, using function returning an array2 By explicit concatenation
Concatenation of columns of arrays[ arr1, arr2, ... , arrn]
Concatenation of rows of arrays[ arr1; arr2; ... ; arrn]
Note: an scalar is also regarded as an array (of size [1,1]).Note 2: arrays must have matching sizes.
Example (Concatenation)
>> A = [1,2;3,4]
A =
1 2
3 4
>> B = [A,A]
B =
1 2 1 2
3 4 3 4
>> C = [A;A]
C =
1 2
3 4
1 2
3 4
Howtos
How to pre-allocate memory
Functions for pre-allocating memory include:
colon (:) – for linear sequences
zeros – all zero array of given size
ones – all one array of given size
rand – random array of given size (equally in [0, 1])
To improve performance arrays should always be pre-allocated!
Example (Functions initializing arrays)
>> A = zeros(3,3);
>> A = ones(4,4,4);
>> size(A)
ans =
4 4 4
>> x = 3:-0.5:1
x =
3.0 2.5 2.0 1.5 1.0
>> A = ones(2)
1 1
1 1
back
Howtos
How to write an m-file script?
1 open a text-editor of your choice or use the editor providedwith Matlab
>> edit myscript
2 Write all your calculations in a text-file with extension “.m”(here myscript.m)
3 Save file in the current working directory(or addpath to search path )
4 Call your script by calling it from the “Command Window”
>> myscript;
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 number2 random number = rand ( 1 ) ;3 f p r i n t f ( ’A random number : %1.4 f :\ n ’ , random number ) ;
back
Howtos
How to use basic syntax: if-clause
if-else block syntax:
1 i f s c a l a r c o n d i t i o n2 e x p r e s s i o n s3 e l s e
4 e x p r e s s i o n s5 end
Relational operators, eg.: == (equals), || (or), && (and), ~ (not)
for details type: help relop
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
back
Howtos
How to use a for-loop
for-loop block syntax:
1 f o r i = a r r a y2 % i==a r r a y ( j ) i n the j−th l oop3 e x p r e s s i o n s4 end
(one can also use break and continue keywords)
Example (for loop)
a=0;f o r i = 1 :100
a = a+i ;end
back
Howtos
How to index arrays
1 Subscript of a matrix:
access the (i , j)-th element of a 2D-matrix W of dimension (m, n)
>> W(i,j) = 1
Note: The first index is always 1 (not 0 as in most other languages)
2 Linear index of a matrix:
access the (i , j)th element of the 2D-matrix W of dimension (m, n)
>> linearidx = i + (j-1)*m;
>> W(linearidx)
3 “Slice” indexing with “:”
access the i-th row and jth column in W , respectively
>> wi = W(i,:)
>> wj = W(:,j)
get all elements as a concatenated column vector
>> W(:)
back
Howtos
How to index arrays (2)
4 Multiple indices
vectors of linear indices can be used
>> W([1, 4, 5 ,6])
access the 1st to 4th rows of the 2D-matrix W of dimension (m, n)
>> W(1:4,:)
access the 2nd (m,n)-slice of a 3D-matrix V of dimension (m, n, p)
>> W(:,:,2)
5 Logical indexing
logical matrices of the same size as W can be used as index (veryuseful)
>> W(W>0)
>> W(find(W>0)) = 1
back
Howtos
Calculating with arrays
1 Element-wise interpretationFor instance, sin cos log etc.Reserved symbols, .* ./ .^
2 “true” matrix interpretation (with dot product)Symbols * / ^ etc.
3 Operations on one specified dimensions of the matrixFor instance, sum mean max etc.
4 Array manipulationseg. reshape repmat permute circshift tril
Example (element-wise product and dot product)
>> A = ones(2,2);
>> A.*A
ans =
1 1
1 1
>> A*A
ans =
2 2
2 2
back
Howtos
Calculating with arrays is
straightforward
however, carefully checkthe size of matricesif element-wise or matrix-like operations are intendedwhich matrix dimension to operate on
Example (compute yi = W xi + b with b, xi ∈ R2)
>> W = [1,0.2;0.4,1];
>> b = [1;2] + 0.1;
>> x = 2*randn(2,1);
>> y = W * x + b
y =
4.5535
1.4856
>> N = 5;
% same as b = [b,b,b,b,b]
>> bi = repmat(b,[1,N]);
>> xi = 2*randn(2,N);
>> xi(:,1) = x;
>> yi = W * xi + bi
yi =
4.5535 2.3126 [..] -0.9021
1.4856 6.8091 [..] -0.1080
back
Howtos
Gamma distribution
The Gamma probability density function is defined as
p(x |k , θ) = 1
Γ(k)θkxk−1e−
xθ
with shape k and scale θ and x ∈ [0,∞).
Example (Gamma random numbers)
>> shape = 2;
>> scale = 0.003;
>> n = 1e7;
>> r = gamrnd(shape,scale,n,1);
>> size(r)
ans =
1000000 1
>> hist(r,1000)
back
Howtos
How to use sparse matrices
In Matlab it is often more efficient (faster) to use sparse matricesinstead of regular matrices if the majority of matrix elements are 0.
To generate a sparse matrix:
>> W = sparse(W);
In general, the syntax for using sparse matrices is the same as forregular 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 the S t i r l i n g Formula2
3 n = 50 ;4
5 n f a c t o r i a l = sqr t (2∗ p i ∗n )∗ ( n/exp ( 1 ) ) ˆ n
back to text
Howtos
Solution to Excercise #2
1 funct ion n f a c t o r i a l = s t i r f a c ( n ) ;2 %compute the S t i r l i n g Formula3
4 n f a c t o r i a l = sqr t (2∗ p i ∗n )∗ ( n/exp ( 1 ) ) ˆ n ;
back to text