我非常接近于将这个Matlab脚本转换成R,并成功地转换了几个类似的脚本。但是,这个我就是找不到这个bug。有人能认出它吗?
测试的一种方法是运行两个脚本并查看情节是否相同。Matlab图是正确的。
请注意,我为R使用了一个Matlab插件,这样一些代码就可以保持灵活。
Matlab:
% Excitatory neurons Inhibitory neurons
Ne=800; Ni=200;
re=rand(Ne,1); ri=rand(Ni,1);
a=[0.02*ones(Ne,1); 0.02+0.08*ri];
b=[0.2*ones(Ne,1); 0.25-0.05*ri];
c=[-65+15*re.^2; -65*ones(Ni,1)];
d=[8-6*re.^2; 2*ones(Ni,1)];
S=[0.5*rand(Ne+Ni,Ne),-rand(Ne+Ni,Ni)];
v=-65*ones(Ne+Ni,1); % Initial values of v
u=b.*v; % Initial values of u
firings=[]; % spike timings
for t=1:1000 % simulation of 1000 ms
I=[5*randn(Ne,1);2*randn(Ni,1)]; % thalamic input
fired=find(v>=30); % indices of spikes
ding = fired;
if ~isempty(fired)
firings=[firings; t+0*fired, fired];
v(fired)=c(fired);
u(fired)=u(fired)+d(fired);
jaja = S(:,fired);
I=I+sum(S(:,fired),2);
end;
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
u=u+a.*(b.*v-u);
end;
plot(firings(:,1),firings(:,2),'.');
xlabel('Time (ms)'); ylabel('Neurons');
R:
require(matlab)
require(ramify)
require(ggplot2)
Ne <- 800
Ni <- 200
re <- rand(Ne,1)
ri <- rand(Ni,1)
# Excitatory neurons # Inhibitory neurons (i.e different column vectors)
a <- matrix(c(0.02*ones(Ne,1), 0.02+0.08*ri))
b <- matrix(c(0.2*ones(Ne,1) , 0.25-0.05*ri))
c <- matrix(c(-65+15*re^2, 65*ones(Ni,1)))
d <- matrix(c(8-6*re^2, 2*ones(Ni,1)))
S <- cbind(0.5*rand(Ne+Ni,Ne),-rand(Ne+Ni,Ni))
v <- -65*ones(Ne+Ni,1) # Initial values of v
u <- b*v # Initial values of u
firings <- matrix(ncol=2) # spike timings
for (t in 1:1000) { # simulation of 1000 ms
I <- matrix(c(5*randn(Ne,1), 2*randn(Ni,1))) # thalamic input
fired <- matlab::find(v>=30) # indices of spikes
if (!isempty(fired)) {
firings <- rbind(firings,cbind(t+0*fired, fired))
v[fired] <- c[fired]
u[fired] <- u[fired]+d[fired]
I <- I + sum(S[,fired])
}
v <- v + 0.5 * (0.04*v^2+5*v+140-u+I)
v <- v + 0.5 * (0.04*v^2+5*v+140-u+I)
u <- u + a * (b*v-u)
}
plot(firings[,1], firings[,2])
图表应该如下所示:
一个问题是点火变量在R中产生一个412369×2矩阵,而在Matlab中得到一个7383x2矩阵。我根本找不到这是在哪里发生的。
发布于 2020-04-09 20:52:53
我检查了R
和MATLAB
的代码,找出了两个你没有正确翻译的地方,应该是
从
c <- matrix(c(-65+15*re^2, 65*ones(Ni,1)))
到c <- matrix(c(-65+15*re^2, -65*ones(Ni,1)))
sum(S[,fired])
到rowSums(S[,fired,drop = FALSE])
下面是我从R
得到的情节
https://stackoverflow.com/questions/61128512
复制相似问题