Can I get rid of this for loop with vectorization?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello,
In this task I'm trying to generate six matrices (m_plot_I, h_plot_I, m_plot_E, h_plot_E, Ge and Gi). The for loop is very simple: as time t passes, the same formula gets performed over and over until we hit niter - 1. I'm quite sure there's a way to make this code more efficient with vectorization, however I can't get my head around this. Could you help me? Thanks!
G_max_chl = 20; % Maximal conductance of chloride
G_max_glu = 30; % Maximal conductance of glutamate
Vm = -70:10:0; % Array of holding potentials
tau_rise_In = 0.15; % Tau rise for inhibition
tau_decay_In = 0.5; % Tau decay for inhibition
tau_rise_Ex = 0.23; % Tau rise for excitation
tau_decay_Ex = 0.7; % Tau decay for excitation
tmax = 15; % Duration of experiment
EGlu = 0; % Reversal potential of glutamate
EChl = -70; % Reversal potential of chloride
% Initialize time
t = 0; % Start
dt = 0.1; % time step duration (ms)
% Total number of time steps in the experiment:
niter = ceil(tmax/dt);
% Initialize values
m_plot_I = zeros(1,niter);
h_plot_I = zeros(1,niter);
m_plot_E = zeros(1,niter);
h_plot_E = zeros(1,niter);
Ge = zeros(1,niter);
Gi = zeros(1,niter);
for ii = 1: niter-1 % For each of the time steps
% Update gating variables for inhibition
m_plot_I(1,ii) = 1 - exp(-t / tau_rise_In); % Activation
h_plot_I(1,ii) = exp(-t / tau_decay_In); % Inactivation
% Update gating variables for excitation
m_plot_E(1,ii) = 1 - exp(-t / tau_rise_Ex); % Activation
h_plot_E(1,ii) = exp(-t / tau_decay_Ex); % Inactivation
% Update conductances
Gi(1,ii) = G_max_chl * (1 - exp(-t / tau_rise_In)) * (exp(-t / tau_decay_In)); % Inhibitory
Ge(1,ii) = G_max_glu * (1 - exp(-t / tau_rise_Ex)) * (exp(-t / tau_decay_Ex)); % Excitatory
t = t+0.1;
end
댓글 수: 0
채택된 답변
Alan Stevens
2021년 2월 8일
Try the following:
G_max_chl = 20; % Maximal conductance of chloride
G_max_glu = 30; % Maximal conductance of glutamate
Vm = -70:10:0; % Array of holding potentials
tau_rise_In = 0.15; % Tau rise for inhibition
tau_decay_In = 0.5; % Tau decay for inhibition
tau_rise_Ex = 0.23; % Tau rise for excitation
tau_decay_Ex = 0.7; % Tau decay for excitation
tmax = 15; % Duration of experiment
EGlu = 0; % Reversal potential of glutamate
EChl = -70; % Reversal potential of chloride
% Initialize time
dt = 0.1; % time step duration (ms)
t = 0:dt:tmax;
% Total number of time steps in the experiment:
niter = ceil(tmax/dt);
% Update gating variables for inhibition
m_plot_I = 1 - exp(-t / tau_rise_In); % Activation
h_plot_I = exp(-t / tau_decay_In); % Inactivation
% Update gating variables for excitation
m_plot_E = 1 - exp(-t / tau_rise_Ex); % Activation
h_plot_E = exp(-t / tau_decay_Ex); % Inactivation
% Update conductances
Gi = G_max_chl * (1 - exp(-t / tau_rise_In)) .* (exp(-t / tau_decay_In)); % Inhibitory
Ge = G_max_glu * (1 - exp(-t / tau_rise_Ex)) .* (exp(-t / tau_decay_Ex)); % Excitatory
댓글 수: 2
Cris LaPierre
2021년 2월 8일
Also a comment that the for loop is not calculating the last value. You stop your loop at 149, but you preallocate your variables with zeros. Because you want 150 elements, your final time is 14.9. I'd be inclined to make it 151 elements and stop at an even 15.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!