How can I run these codes in a for loop?
Mostra commenti meno recenti
Hi! I understand that the variables should not be named dynamically. So, I would like to have an idea on how can I run these kind of code in a for loop without repeating the variable inthe future. A good idea will help me to think differently and more efficiently. Thank you in advance!!
idx_0 = hour(result.HourSeries1)==00;
m0 = mean(Climatology1.H1(idx_0,:))
idx_1 = hour(result.HourSeries1)==01;
m1 = mean(Climatology1.H1(idx_1,:))
idx_2 = hour(result.HourSeries1)==02;
m2 = mean(Climatology1.H1(idx_2,:))
idx_3 = hour(result.HourSeries1)==03;
m3 = mean(Climatology1.H1(idx_3,:))
idx_4 = hour(result.HourSeries1)==04;
m4 = mean(Climatology1.H1(idx_4,:))
I tried to do this way but it shows "Array indices must be positive integers or logical values."
x = [0:23];
for i =1:24;
idx{i-1} = hour(result.HourSeries1)==i-1;
m{i-1} = mean(Climatology1.H1(idx{i-1},:))
end
Risposta accettata
Più risposte (1)
for i = 0:4
idx(i+1) = hour(result.HourSeries1)==strcat('0',string(i));
m(i+1) = mean(Climatology1.H1(idx(i+1),:));
end
4 Commenti
Ashfaq Ahmed
il 28 Feb 2023
Modificato: Ashfaq Ahmed
il 28 Feb 2023
if i < 10
str = strcat('0',string(i));
else
str = string(i);
end
And to compare strings, you shouldn't use ==, but strcmp.
Voss
il 28 Feb 2023
str = sprintf("%02d",i);
Ashfaq Ahmed
il 28 Feb 2023
Categorie
Scopri di più su Environment and Settings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!