How to loop the date vector, that the restrictions are the months,years and hours??

1 visualización (últimos 30 días)
%for k=xa:1:y2 xa is the first year of the data that i use a function to discover it and y2 is the last year
ka = find(IntervaloHorarioDateVec(:,1) == 2015);
kb = find(IntervaloHorarioDateVec(:,1) == 2016);
kc = find(IntervaloHorarioDateVec(:,1) == 2017);
kd = find(IntervaloHorarioDateVec(:,1) == 2018);
%1-12
%for m = 1:12
ke = find(IntervaloHorarioDateVec(:,2) == 1);
kf = find(IntervaloHorarioDateVec(:,2) == 2);
kg = find(IntervaloHorarioDateVec(:,2) == 3);
kh = find(IntervaloHorarioDateVec(:,2) == 4);
ki = find(IntervaloHorarioDateVec(:,2) == 5);
kj = find(IntervaloHorarioDateVec(:,2) == 6);
kk = find(IntervaloHorarioDateVec(:,2) == 7);
kl = find(IntervaloHorarioDateVec(:,2) == 8);
km = find(IntervaloHorarioDateVec(:,2) == 9);
kn = find(IntervaloHorarioDateVec(:,2) == 10);
ko = find(IntervaloHorarioDateVec(:,2) == 11);
kp = find(IntervaloHorarioDateVec(:,2) == 12);
kq = ismember(ka,ke,'rows');
kr = find(kq);
ks = ismember(ka,kf,'rows');
kt = find(ks);
ku = ismember(ka,kg,'rows');
kv = find(kv);
kw = ismember(ka,kh,'rows');
kx = find(kw);
ky = ismember(ka,ki,'rows');
kz = find(ky);
kaa = ismember(ka,kj,'rows');
kab = find(kaa);
kac = ismember(ka,kk,'rows');
kad = find(kac);
kae = ismember(ka,kl,'rows');
kaf = find(kae);
kag = ismember(ka,km,'rows');
kah = find(kag);
kai = ismember(ka,kn,'rows');
kaj = find(kai);
kak = ismember(ka,ko,'rows');
kal = find(kal);
kam = ismember(ka,kp,'rows');
kan = find(kan);
... doing this until 2018
% and for the hours of the day...
k1 = find(IntervaloHorarioDateVec(:,2) == 0);
k2 = find(IntervaloHorarioDateVec(:,2) == 1);
k3 = find(IntervaloHorarioDateVec(:,2) == 2);
k4 = find(IntervaloHorarioDateVec(:,2) == 3);
k5 = find(IntervaloHorarioDateVec(:,2) == 4);
k6 = find(IntervaloHorarioDateVec(:,2) == 5);
k7 = find(IntervaloHorarioDateVec(:,2) == 6);
k8 = find(IntervaloHorarioDateVec(:,2) == 7);
k9 = find(IntervaloHorarioDateVec(:,2) == 8);
k10 = find(IntervaloHorarioDateVec(:,2) == 9);
k11 = find(IntervaloHorarioDateVec(:,2) == 10);
k12 = find(IntervaloHorarioDateVec(:,2) == 11);
k13 = find(IntervaloHorarioDateVec(:,2) == 12);
k14 = find(IntervaloHorarioDateVec(:,2) == 13);
k15 = find(IntervaloHorarioDateVec(:,2) == 14);
k16 = find(IntervaloHorarioDateVec(:,2) == 15);
k17 = find(IntervaloHorarioDateVec(:,2) == 16);
k18 = find(IntervaloHorarioDateVec(:,2) == 17);
k19 = find(IntervaloHorarioDateVec(:,2) == 18);
k20 = find(IntervaloHorarioDateVec(:,2) == 19);
k21 = find(IntervaloHorarioDateVec(:,2) == 20);
k22 = find(IntervaloHorarioDateVec(:,2) == 21);
k23 = find(IntervaloHorarioDateVec(:,2) == 22);
k24 = find(IntervaloHorarioDateVec(:,2) == 23);
Lia = ismember(k1,kr);
Idrow = find(Lia);
Lia = ismember(k1,kr);
Idrow = find(Lia);
Lia = ismember(k1,kr);
Idrow = find(Lia);
Lia = ismember(k1,kr);
Idrow = find(Lia);
Lia = ismember(k1,kr);
Idrow = find(Lia);
Lia = ismember(k1,kr);
Idrow = find(Lia);
Lia = ismember(k1,kr);
Idrow = find(Lia);
...to 00:00 until 24:00
... going thru all the years 2015-2018,months 1-12, hours 0-23h
%So i want to create a loop, that I can find 00 hours in the year, let's say 2015 of my data, or 2017, or both,
%and in that year i want to find the month, let's say January(01<-number of the month), or March(03),
%and in that YEAR and MONTH, i want to find the position of the 00:00 Hour or 12:00, in a data that is hourly
%so i want to find all the data that are in 00:00hour of all the first month of the first year, the 01:00 hour of the first month
%and so on til 23:00hour, the same with february to december until the end of y2(the last year).
  1 comentario
Paulo Oliveira
Paulo Oliveira el 31 de En. de 2019
I'm trying doing this, but it's not allocating anything in kf, because ka is receiving just the last year data, and the months is receiving just the 2017 of december(12), because the 2018 data doesn't end at december, and ke is just receiving the last hour of the day.
%2015-2018
for k=xa:1:y2
ka = find(IntervaloHorarioDateVec(:,1) == k);
%1-12
for m = 1:12
kb = find(IntervaloHorarioDateVec(:,2) == m);
kc = ismember(ka,kb,'rows');
kd = find(kc);
for n = 0:1:23
ke = find(IntervaloHorarioDateVec(:,4) == n);
kf = ismember(kd,ke,'rows');
kg = find(kc1);
end
end
end

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 31 de En. de 2019
Editada: Jan el 31 de En. de 2019
Logical indexing:
for k = xa:y2
ka = (IntervaloHorarioDateVec(:,1) == k); % No FIND
for m = 1:12
kb = (IntervaloHorarioDateVec(:,2) == m);
for n = 0:23
ke = (IntervaloHorarioDateVec(:,4) == n);
match = find(ka & kb & ke);
...
end
end
end
Or:
for k = xa:y2
for m = 1:12
for n = 0:23
ke = (IntervaloHorarioDateVec(:,4) == n);
match = ismember(IntervaloHorarioDateVec(:, [1,2,4]), [ka, kb, ke], 'rows');
...
end
end
end

Más respuestas (1)

Paulo Oliveira
Paulo Oliveira el 1 de Feb. de 2019
Editada: Paulo Oliveira el 1 de Feb. de 2019
For some reason, ka is OK and is running to the last year, but kb isnt running to the last valor of m in the last year, 'cause the last year doesnt have december(valor of m=12), so the For for kb is only looping to the previous valor of y2 that have valor of m = 12, so, How could I use For to continue looping, to lets say year 2015 and loop every month of that year and every hour of that year and go to the next year and loop to every month and hour to that year and so on, until get's to y2, I tryed to use for and continue, to doenst break when m~=12, but isnt running. And another problem is that I want to allocate in a cell array all that finded in match, in every loop.
and i'm using this code now,
%lets say sadsize = 300 for example, that i wanted to allocate in 300 lines of the cell array
for k=1:sadsize
for k=xa:y2
ka = (IntervaloHorarioDateVec(:,1) == k);
for m = 1:12
kb = (IntervaloHorarioDateVec(:,2) == m);
for n = 0:23
ke = (IntervaloHorarioDateVec(:,4) == n);
match = find(ka & kb & ke);
continue
end
continue
end
continue
end
correlacaovalordiario = CorrelacaoHorariaArray(match);
sada{k} = correlacaovalordiario;
end
is even running, but it's allocating just the last data of match in sada
  2 comentarios
Jan
Jan el 1 de Feb. de 2019
The continue statements do not do anything here. They are a short cut to step to the start of the loop. But when you put them as last statement of a loop, this happens anyway.
The shown code does not work at all: The obtained indices in match are overwritten repeatedly. Of course you have to used them directly after they have been created. I'm still not sure what you want to achieve, so another guess:
sada = cell(1, sadsize);
index = 0;
for k=xa:y2
ka = (IntervaloHorarioDateVec(:,1) == k);
for m = 1:12
kb = (IntervaloHorarioDateVec(:,2) == m);
for n = 0:23
ke = (IntervaloHorarioDateVec(:,4) == n);
match = find(ka & kb & ke);
index = index + 1;
sada{index} = CorrelacaoHorariaArray(match);
end
end
end
Paulo Oliveira
Paulo Oliveira el 1 de Feb. de 2019
Haha, sorry for that, but it worked nicely for n = 0:23, now i wanted that index = index + 1 worked too in m = 1:12 to write in the next cell after the first sada(in 0:23), to continue the loop withouth overwritten sada.

Iniciar sesión para comentar.

Categorías

Más información sobre Time Series Objects en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by