Third for loop runs, second and third loops do not

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

 Akzeptierte Antwort

Torsten
Torsten am 7 Okt. 2022
Verschoben: Voss am 1 Jan. 2023

0 Stimmen

If val_T were 1, only the third loop was entered. But we don't know the value of val_T.

1 Kommentar

jchooch
jchooch am 7 Okt. 2022
Verschoben: Voss am 1 Jan. 2023
OMG. Yes, this was the problem! Thank you so much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 7 Okt. 2022

Verschoben:

am 1 Jan. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by