Filter löschen
Filter löschen

For loop not working

1 Ansicht (letzte 30 Tage)
Joel Schelander
Joel Schelander am 12 Mär. 2021
Kommentiert: Joel Schelander am 12 Mär. 2021
I am looping over one year in minutes. However the script stops looping values at T=49716 for some reason. Then element j=1248 in matrix B
B has one column saying which minute of the year that the value in the second column appears (13377x2 double)
How can I make it loop for the entire year?
for T=1:525600
if T~=B(j,1)
A(T)=0;
elseif T==B(j,1)
A(T)=B(j,2);
j=j+1;
end
end

Akzeptierte Antwort

Jan
Jan am 12 Mär. 2021
The loop does run over all T. Why do you think, that the loop stops?
A = zeros(1, 525600); % Pre-allocation, faster and simpler code
j = 1; % Assumption
for T = 1:525600
if T == B(j, 1)
A(T) = B(j, 2);
j = j + 1;
end
end
Equivalent code without a loop:
A = zeros(1, 525600);
A(B(:, 1)) = B(:, 2):
  1 Kommentar
Joel Schelander
Joel Schelander am 12 Mär. 2021
It worked without the loop, thank you Jan

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

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

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by