How to find days that are togheter
Show older comments
Hi! I need help to find if there are 5 consequtive days with the temperature over 25 degrees in my data.
This is what I have now. I have taken out the days where the temperature has been over 25 degrees. Now I want to see if there has been any heat waves occuring. The definition of a heat wave is that there are five consecutive days with the maximum temperature over 25 degrees
Data=importdata('');
%Define different parameters
year=Data(:,1);
month=Data(:,2);
day=Data(:,3);
time=Data(:,4);
temp=Data(:,5);
%
%Find the number of times each year the temperature is over 25
%degrees
M=[];
S=find(temp>25)
M=[year(S),month(S),day(S),S];
M
%
Example of how M looks like
- Year Month Day
- 1991 7 3
- 1991 7 3
- 1991 7 4
- 1991 7 4
- 1991 7 4
- 1991 7 5
- 1991 7 5
- 1991 7 5
- 1991 7 5
- 1991 7 6
- 1991 7 6
- 1991 7 6
- 1991 7 7
- 1991 7 7
- 1991 7 7
- 1991 7 8
- 1991 7 8
- 1991 7 8
As there is 24 values for each day (one per hour) one day can show several values.
I would like to find the periods when a heat wave has occured, but also plot how many heatwaves there has been in one year.
Thankful for any help that can leed me on the right way to the answer.
Best regards Marie
Answers (2)
Marc Jakobi
on 5 Nov 2013
%Get rid of "multiple-days"
[a,~] = size(M);
M2 = M(1,:);
for i = 2:a
if M(i,3) ~= M(i-1,3)
M2 = [M2; M(i,:)];
end
end
%Extract groups of >= 5 consecutive days
[a,~] = size(M2);
Consec = [];
for i = 5:a
if M2(i,3) == M2(i-4,3)
Consec = [Consec; M2(i-5:i,:)];
end
end
Hope this helps. It's getting a little late where I am to think of a way to plot it. You could create a "clone" of M using datenum(), such as Mnum = datenum(M) and creating a vector from
WholeYearNum = (datenum(1991 1 1):1./*number of hours in that year*:datenum(1991 12 30))
Use that vector as a reference so you know where to fill in the gaps with zeros and plot it with the amount of hours in that year.
3 Comments
Marc Jakobi
on 6 Nov 2013
Edited: Marc Jakobi
on 6 Nov 2013
I just realized my %Extract groups of >= 5 consecutive days won't work if the heat wave goes on for longer than 5 days.
%Get rid of "multiple-days"
[a,~] = size(M);
M2 = M(1,:);
for i = 2:a
if M(i,3) ~= M(i-1,3)
M2 = [M2; M(i,:)];
end
end
%Extract groups of >= 5 consecutive days
[a,~] = size(M2);
Consec = [];
for i = 5:a
if M2(i,3) == M2(i-4,3)
n = 5; m = i;
for k = i+1:a
if M2(k,3) == M2(k-1,3)
n = n+1; m = k;
else
break;
end
end
Consec = [Consec; M2(i-n:m,:)];
end
end
This should extract heat waves that go on for longer than five days, too.
Marie
on 6 Nov 2013
Marc Jakobi
on 30 Nov 2013
Edited: Marc Jakobi
on 30 Nov 2013
Sorry for getting back to you so late. Somehow I haven't been receiving e-mails from MathWorks when I get a reply lately...
I don't have access to Matlab right now so I can't give you a definite answer. But I checked my code and it looks like I made a stupid mistake. I tried to extract groups larger than five days in the "consec" loop using the vector M2. But M2 is the vector I created to get rid of the multiple days, so of course consec will remain empty. Try using M instead of M2 in the second loop. a will have to be adjusted to size(M) instead of M2 as well.
Andrei Bobrov
on 6 Nov 2013
Edited: Andrei Bobrov
on 6 Nov 2013
t0 = temp(:)' >= 25;
tt = [strfind([0,t0],[0 1]);strfind([t0,0],[1 0])];
out = cellfun(@(x)Data(x(1):x(2),:),num2cell(tt(:,diff(tt) >= 119),1),'un',0);
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!