% Function media_qualquer_coisa2
% Calcula media e mediana de qualquer coisa, com time stamp no INÍCIO do período
% NaNs na matriz de entrada são filtrados
% Written by Luciana Rizzo
%
% input: data_mqc (time | values to be averaged)
% output: stat (time | var1_avg| var1_std| var1_median| var1_p25| var1_p75| var1_max| var1_min| var1_#| ... same for var2, etc )
% statorder (time | avgs(var1,var2,...) | std(var1,var2,...) | medians(var1,var2,...) | p25(var1,var2,...) | p75(var1,var2,...) | max(var1,var2,...) | min(var1,var2,...) | #(var1,var2,...) |
% tolerancia para o final de cada período em dias (ex: 30seg = 0.5/60/24)
% (se tol<0, diminui o período de média; se tol>0, aumenta o período de média)
%
function [stat,statorder] = fmedia_qualquer_coisa2(data_mqc,first,last,delta,tol)
tempo_mqc=first:delta:last; % average time line
tempo_mqc=tempo_mqc';
nvar=size(data_mqc,2)-1;
stat=zeros(length(tempo_mqc),8*nvar+1);
stat(:,1)=tempo_mqc;
%
for imqc=1:length(tempo_mqc)
if(mod(imqc,1000)==0)
disp([num2str(100*imqc/length(tempo_mqc)),'%'])
end
amqc=find(data_mqc(:,1)>=(tempo_mqc(imqc)+tol) & data_mqc(:,1)<(tempo_mqc(imqc)+delta+tol));
if (~isempty(amqc))
if (length(amqc)==1) % only one record in the period
for jmqc=1:nvar
stat(imqc,8*jmqc-6)=data_mqc(amqc,jmqc+1); %avg
stat(imqc,8*jmqc-5)=NaN; %std
stat(imqc,8*jmqc-4)=data_mqc(amqc,jmqc+1); %median
stat(imqc,8*jmqc-3)=NaN; %p25
stat(imqc,8*jmqc-2)=NaN; %p75
stat(imqc,8*jmqc-1)=data_mqc(amqc,jmqc+1); %max
stat(imqc,8*jmqc)=data_mqc(amqc,jmqc+1); %min
stat(imqc,8*jmqc+1)=1; %num
end
else
for jmqc=1:nvar
aux_mqc=data_mqc(amqc,jmqc+1);
aux_mqc=aux_mqc(~isnan(aux_mqc)); % remove enventuais NaNs de cada variável
if(~isempty(aux_mqc))
stat(imqc,8*jmqc-6)=mean(aux_mqc); %avg %alternativa: nanmean
stat(imqc,8*jmqc-5)=std(aux_mqc); %std
stat(imqc,8*jmqc-4)=prctile(aux_mqc,50); %median
stat(imqc,8*jmqc-3)=prctile(aux_mqc,25); %p25
stat(imqc,8*jmqc-2)=prctile(aux_mqc,75); %p75
stat(imqc,8*jmqc-1)=max(aux_mqc); %max
stat(imqc,8*jmqc)=min(aux_mqc); %min
stat(imqc,8*jmqc+1)=length(aux_mqc); %num
else
stat(imqc,8*jmqc-6:8*jmqc+1)=NaN;
end
clear aux_mqc
end
end
else
stat(imqc,2:size(stat,2))=NaN;
end
end
%
statorder(:,1)=stat(:,1); % time
jj=2;
for jmqc=2:8:nvar*8-6 % averages
statorder(:,jj)=stat(:,jmqc);
jj=jj+1;
end
%jj=nvar+2;
for jmqc=3:8:nvar*8-5 % std
statorder(:,jj)=stat(:,jmqc);
jj=jj+1;
end
%jj=2*nvar+2;
for jmqc=4:8:nvar*8-4 % medians
statorder(:,jj)=stat(:,jmqc);
jj=jj+1;
end
for jmqc=5:8:nvar*8-3 % p25
statorder(:,jj)=stat(:,jmqc);
jj=jj+1;
end
for jmqc=6:8:nvar*8-2 % p75
statorder(:,jj)=stat(:,jmqc);
jj=jj+1;
end
for jmqc=7:8:nvar*8-1 % max
statorder(:,jj)=stat(:,jmqc);
jj=jj+1;
end
for jmqc=8:8:nvar*8-0 % min
statorder(:,jj)=stat(:,jmqc);
jj=jj+1;
end
for jmqc=9:8:nvar*8+1 % num
statorder(:,jj)=stat(:,jmqc);
jj=jj+1;
end
%
clear tempo_mqc last first delta amqc imqc jmqc data_mqc nvar jj