Can I get rid of this for loop with vectorization?

1 vue (au cours des 30 derniers jours)
Samuele Bolotta
Samuele Bolotta le 8 Fév 2021
Commenté : Samuele Bolotta le 8 Fév 2021
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

Réponse acceptée

Alan Stevens
Alan Stevens le 8 Fév 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 commentaires
Cris LaPierre
Cris LaPierre le 8 Fév 2021
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.
Samuele Bolotta
Samuele Bolotta le 8 Fév 2021
Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by