How can I count it by months?

3 visualizaciones (últimos 30 días)
Jaaaaaaaai
Jaaaaaaaai el 11 de Sept. de 2015
Editada: dpb el 12 de Sept. de 2015
Hi all! I haven't been using Matlab for awhile and I'm struggling a bit now. The following is a dataset of events, and I was wondering if you guys could lend me a hand on constructing a loop code on counting the number of events per month. For example, it would be 2 for Dec05, 1 for Jan06 and 1 for Feb06... and so on. Thanks in advance!
ID TIME DATA YEAR MONTH
1803 38699.29973 1.9 2005 12
1804 38704.26328 2.2 2005 12
1805 38729.27574 1.4 2006 1
1806 38772.3945 1.6 2006 2
1807 38789.94187 2.2 2006 3
The following is a portion of the code that I have:
i = 1;
j = 1;
while data(i,5) == data(i+1,5)
i = i + 1;
if data(i,5) ~= data(i+1,5)
month_record(j,1) = month(data(i,2));
month_record(j,2) = i;
i = i + 1;
continue
end
if data(i+1,5) == NaN
break
end
end

Respuestas (1)

dpb
dpb el 11 de Sept. de 2015
Editada: dpb el 12 de Sept. de 2015
This will be much easier if you convert the month, day columns into date numbers. I don't have the newer table by which you could then use the new datetime and splitapply, but the idea can be easily illustrated for just plain arrays...
>> y=[2005 2005 2006 2006 2006];
>> m=[12 12 1:3];
>> dn=datenum(y,m,1).'; % convert to date numbers
>> u=unique(dn); % the unique dates in the total list
>> n=hist(dn,u) % bin over those values
n =
2 1 1 1
>> ds=dataset(u,datestr(u,'mmm yyyy'),n.','VarNames',{'DateNum';'Date';'Events'})
ds =
DateNum Date Events
7.3265e+05 Dec 2005 2
7.3268e+05 Jan 2006 1
7.3271e+05 Feb 2006 1
7.3274e+05 Mar 2006 1
>>
Last simply uses the Statistics Toolbox dataset to display the disparate values similar to what the new table class can do...

Categorías

Más información sobre Dates and Time 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