optimising for loop with vectorization?

Hello, I have a code with 4 for loop, can you please help me to vectorise it. I tried to work with multi-dimensional array but I am not sure about it. Thank you.
dt = 0.1;
tmax = 61000;
tspan = 0:dt:tmax;
N = 96;
Q = zeros(N,tmax);
tao = 100;
gamma = 0.641;
JN = 0.2609;
a=270.0;
b=108.0;
cx=0.154;
SC=rand(N,N);
for omega = 10:-1:1
w = 0.03*(omega-1)+0.73;
for noise = 10:-1:1
n = 0.001*(noise-1)+0.001;
for I0 = 10:-1:1
ex = 0.01*(I0-1)+0.25;
for G = 10:-1:1
c = 0.1*(G-1)+0.1;
y=0.001*ones(N,1);
nn=1;
j=0;
for k=2:1:length(tspan)
x = ex + JN*(w*y + c*(SC*y));
xx=a*x-b;
r=xx./(1-exp(-cx*xx));
y = y+dt*(-y/tao+(1-y)*gamma.*r./1000.)+sqrt(dt*2)*n;
y(y>1) = 1;
y=y.*(y>=0);
j=j+1;
if j==10
Q(:,nn)=y;
nn=nn+1;
j=0;
end
end
QQ(:,:,omega,noise,I0,G)=Q;
end
end
end
end
and here is what i tried to do:
[y,omega,noise,I0,G]=ndgrid(0.001*ones(N,1),1:2,1:2,1:2,1:2);
w = 0.03*(omega-1)+0.73;
n = 0.001*(noise-1)+0.001;
ex = 0.01*(I0-1)+0.25;
c = 0.1*(G-1)+0.1;
nn=1;
j=0;
alpha=0.001*ones(N,1);
for k=2:1:length(tspan)
x = ex + JN*(w.*y + c.*(emp96(sub).SC*alpha));
xx=a*x-b;
r=xx./(1-exp(-cx*xx));
y = y+dt*(-y/tao+(1-y)*gamma.*r./1000.)+sqrt(dt*2)*n;
y(y>1) = 1;
y=y.*(y>=0);
j=j+1;
if j==10
Q(:,nn,1:2,1:2,1:2,1:2)=y(:,1:2,1:2,1:2,1:2);
nn=nn+1;
j=0;
end
end

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by