Subsetting array of dates according to recessions in Matlab -
i subset array of serial numbers representing dates, according recession periods, can calculate mean()
on these periods. following example illustrate this:
datearray = transpose((1:20000)+678420); rnddata = normrnd(0.003,0.05,19999,1); cumdata = cumprod([1;rnddata+1]); data = [datearray cumdata]; load data_recessions.mat %native `econometrics toolbox` dataset % loads 2 column double array of start dates in first column , corresponding end dates in second column. plot(data(:,1),data(:,2)) set(gca(),'yscale','log'); recessionplot()
i want calculate mean() on grey bars above. dates indicating these periods in recessions array. how do efficiently?
assuming recession dates in 2 column matrix called d
m = arrayfun(@(x)(mean(data(find(data(:,1)==d(x,1)):find(data(:,1)==d(x,2)),2))), 1:size(d,1))
or loop more efficient:
m = nan(size(d,1),1); x = 1:size(d,1) first = find(data(:,1)==d(x,1)) last = find(data(:,1)==d(x,2)) m(x) = mean(data(first:last, 2)) end
Comments
Post a Comment