Third for loop runs, second and third loops do not

1 vue (au cours des 30 derniers jours)
jchooch
jchooch le 7 Oct 2022
Déplacé(e) : Voss le 1 Jan 2023
Hi there,
I have searched all over Matlab Answers for a solution to this problem but can't find one.
I have a function which contains the 3 for loops below. When I run the function, only the third for loop is executed (which I have diagnosed using the disp statements).
Is there a reason why these three loops, in this order, would not all be executed? I have been using Matlab for a number of years now but am completely baffled and confused. When I changed the first for loop to "tt = 1:val_T", that loop ran, so I suspect it has something to do with the indexing? Unfortunately, in many cases in my code the 1st index is special, so I need my loops to run from 2 onwards, so I would love a solution which does not require me to change all of my indexing to start from 1 (I have no idea how I could do such a thing, and have no idea why Matlab would require such a thing!).
I would very much appreciate any help, and am happy to provide the full script if there are no problems with the code below. Thank you so much!
for tt = 2:val_T
disp('in first tt loop')
inputN(:, tt) = iWN(:, tt) + (inputN(:, tt-1) - iWN(:, tt))*exp(-dtModel);
end
for tt = 2:val_T-1
disp('in second tt loop')
val_R(:, tt) = tanh(val_H(:, tt));
for channel = 1:C
if val_inputs(channel, tt) == 1
stimuli_epochs(channel) = 1;
end
end
val_JR = J * val_R(:, tt) + inputN(:, tt);
for channel = 1:C
neuronal_inputs(channel, :, tt) = amp_rgc * val_inputs(channel, tt) .* input_weights(channel);
end
for channel = 1:C
val_JR = val_JR + transpose(neuronal_inputs(channel, :, tt));
end
disp('val_JR: ')
disp(val_JR)
val_H(:, tt+1) = val_H(:, tt) + dtModel * (- val_H(:, tt) + val_JR);
end
for tt = 1:val_T
disp('in third tt loop')
% check if there is a data timepoint to match with model timepoint
if model_speed == 1
data_index = (tt + model_speed - 1) / model_speed;
JR_err = val_JR - val_data(:, data_index);
R_err = val_R(:, tt) - val_data(:, data_index);
H_err = val_H(:, tt) - val_data(:, data_index);
elseif mod(tt, model_speed) == 1
data_index = (tt + model_speed - 1) / model_speed;
JR_err = val_JR - val_data(:, data_index);
R_err = val_R(:, tt) - val_data(:, data_index);
H_err = val_H(:, tt) - val_data(:, data_index);
end
JR_mse = JR_mse + mean(JR_err.^2);
R_mse = R_mse + mean(R_err.^2);
H_mse = H_mse + mean(H_err.^2);
end

Réponse acceptée

Torsten
Torsten le 7 Oct 2022
Déplacé(e) : Voss le 1 Jan 2023
If val_T were 1, only the third loop was entered. But we don't know the value of val_T.
  1 commentaire
jchooch
jchooch le 7 Oct 2022
Déplacé(e) : Voss le 1 Jan 2023
OMG. Yes, this was the problem! Thank you so much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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