find and store index for consecutive numbers
18 views (last 30 days)
Consider the following:
Jday = datenum('2009-01-01 00:00','yyyy-mm-dd HH:MM'):1:...
datenum('2009-12-31 23:00','yyyy-mm-dd HH:MM');
DateV = datevec(Jday);
dat = 1+(10-1).*rand(length(Jday),1);
noise = 10*sin(2*pi*Jday/32)+20;
for i = 1:size(dat,2);
dat2(:,i) = dat(:,i)+noise';
nan_dat = floor(100+(300-1).*rand(50,1));
dat2(nan_dat) = nan;
From 'dat2' I would like to find the maximum number of days in each month (denoted by DateV(:,2)) where dat2 is greater than 30. In addition I would like the number of days to be equal in each month so if we have 10 values greater than 30 in month 1 and then only 5 values greater than 30 in month 2 then we should only take the first 5 values greater than 30 in both months. This should then be applied to all months. Note: missing values are present in this series.
ChristianW on 11 Mar 2013
I guess your problem is identifying same areas in a vector. Therefore you may use this function:
Isame = @(x) [1; find(diff(x))+1; length(x)]; % index for same areas
An example is
x = [2 2 2 2 1 6 6 6 0]';
Idx = Isame(x) % Idx = [1 5 6 9 9]
The first area starts at index 1, the second at index 5, etc. As last index I added the x(end)-index. This is helpful in a loop.
In this way you can loop the same area's instead of every x element. I would loop the months like that and also calculate area's greater than 30 (Isame(X>30)). For the area-length (number of days) use diff(Isame(X>30)). Do not forget there are true and false area's for X>30.