MATLAB Answers

Can I get rid of this for loop with vectorization?

2 views (last 30 days)
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

Accepted Answer

Alan Stevens
Alan Stevens on 8 Feb 2021
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 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!