function B=annsumavg(A,n)
%Translate quarterly data into annual data

%*A* is a matrix with data at a quarterly frequency in columns. 
%The first column are Matlabdates
%First, there are *n* series for which annual averages need to be computed
%(this INcludes the Matlabdates)
%Further to the right are the series for which annual sums need to be
%computed

rows=size(A,1);
cols=size(A,2);

B=nan(rows/4+2,cols);

%Compute the annual average for the first n series (including the dates)
for i=1:n
    for j=4:4:rows
        %B(j/4,i)=(A(j-3,i)+A(j-2,i)+A(j-1,i)+A(j,i))/4;
        B(j/4,i)=mean(A(j-3:j,i));
    end
end

%Compute the annual sums for the other series
for i=n+1:cols
    for j=4:4:rows
        %B(j/4,i)=A(j-3,i)+A(j-2,i)+A(j-1,i)+A(j,i);
        B(j/4,i)=sum(A(j-3:j,i));
    end
end
