Borrar filtros
Borrar filtros

How can I run these codes in a for loop?

1 visualización (últimos 30 días)
Ashfaq Ahmed
Ashfaq Ahmed el 28 de Feb. de 2023
Comentada: Campion Loong el 13 de Mzo. de 2023
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

Respuesta aceptada

Voss
Voss el 28 de Feb. de 2023
Editada: Voss el 28 de Feb. de 2023
If the means are all scalars
m = zeros(5,1);
for ii = 1:5
idx = hour(result.HourSeries1) == ii-1;
m(ii) = mean(Climatology1.H1(idx,:));
end
Otherwise
m = zeros(5,size(Climatology1.H1,2));
for ii = 1:5
idx = hour(result.HourSeries1) == ii-1;
m(ii,:) = mean(Climatology1.H1(idx,:), 1);
end
  3 comentarios
Voss
Voss el 28 de Feb. de 2023
The built-in hour function returns a double, so I assume the hour you're using is your own function or variable.
idx = strcmp(hour(result.HourSeries1),sprintf('%02d',ii-1))
Campion Loong
Campion Loong el 13 de Mzo. de 2023
Adding to @Voss's answer, this is basically subscripting with "hour" in result.HourSeries1. Depending on whether that is a datetime or text, I would prefer holding the "Climatology" data in a timetable or table so your data labels (i.e. time, text) go with your data, like this (if you use datetime)
% Making up random multi-column data for H1
ResultTimes = datetime(2023,3,13) + hours(1:1000)';
Climatology = timetable(ResultTimes, rand(1000,5),'VariableNames',"H1")
Climatology = 1000×1 timetable
ResultTimes H1 ____________________ _____________________________________________________________ 13-Mar-2023 01:00:00 0.84146 0.79722 0.82259 0.66337 0.65927 13-Mar-2023 02:00:00 0.67373 0.89206 0.58789 0.019026 0.66615 13-Mar-2023 03:00:00 0.88871 0.4429 0.046643 0.40312 0.23974 13-Mar-2023 04:00:00 0.3896 0.68458 0.30343 0.40802 0.7763 13-Mar-2023 05:00:00 0.439 0.53479 0.88126 0.19547 0.12425 13-Mar-2023 06:00:00 0.62536 0.44538 0.67953 0.84661 0.028268 13-Mar-2023 07:00:00 0.77342 0.29515 0.9852 0.16639 0.63207 13-Mar-2023 08:00:00 0.76506 0.46657 0.59155 0.37077 0.52225 13-Mar-2023 09:00:00 0.6151 0.41233 0.91669 0.58814 0.19279 13-Mar-2023 10:00:00 0.81799 0.22129 0.95078 0.087836 0.56349 13-Mar-2023 11:00:00 0.046164 0.73673 0.84577 0.67314 0.39125 13-Mar-2023 12:00:00 0.66193 0.093104 0.021965 0.13976 0.70648 13-Mar-2023 13:00:00 0.045651 0.78472 0.6179 0.78254 0.74244 13-Mar-2023 14:00:00 0.0089332 0.28782 0.48349 0.50385 0.23653 13-Mar-2023 15:00:00 0.22407 0.03468 0.75578 0.57565 0.24565 13-Mar-2023 16:00:00 0.2304 0.056612 0.17365 0.93395 0.48146
% Pull out data based on time-of-day (e.g. 3AM)
Climatology( hour(Climatology.ResultTimes) == 3, :).H1
ans = 42×5
0.8887 0.4429 0.0466 0.4031 0.2397 0.1195 0.3022 0.0428 0.5548 0.8576 0.5340 0.6981 0.6842 0.1122 0.8263 0.0722 0.5172 0.1391 0.0341 0.3207 0.4562 0.6380 0.0116 0.8380 0.2409 0.9217 0.0622 0.4751 0.9748 0.9114 0.8217 0.4114 0.9609 0.5626 0.0504 0.8715 0.5308 0.1174 0.4100 0.5577 0.3256 0.1523 0.9762 0.7915 0.0644 0.7501 0.0962 0.4406 0.8895 0.5859
% Now applying @Voss's answer for non-scalar means
m = zeros(24,size(Climatology.H1,2));
ResultTimes_HourOfDay = hour(Climatology.ResultTimes);
for ii = 1:24 % note HoD ranges from 0-23
m(ii,:) = mean( Climatology(ResultTimes_HourOfDay == ii-1,:).H1 );
end
m
m = 24×5
0.4870 0.6044 0.4125 0.4095 0.4529 0.5072 0.5466 0.5409 0.5480 0.5396 0.5507 0.5194 0.5247 0.4759 0.5053 0.5690 0.5028 0.4895 0.4708 0.5433 0.5019 0.5100 0.4935 0.4068 0.5119 0.5362 0.4813 0.4724 0.4415 0.5209 0.5907 0.4943 0.5441 0.4915 0.5329 0.5106 0.4688 0.5590 0.5524 0.5145 0.4397 0.5091 0.5410 0.4844 0.5222 0.5857 0.4629 0.4559 0.4624 0.4481

Iniciar sesión para comentar.

Más respuestas (1)

Torsten
Torsten el 28 de Feb. de 2023
Editada: Torsten el 28 de Feb. de 2023
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 comentarios
Voss
Voss el 28 de Feb. de 2023
str = sprintf("%02d",i);
Ashfaq Ahmed
Ashfaq Ahmed el 28 de Feb. de 2023
Thank you both. You guys are awesome.

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by