zqq0ew0的个人博客分享 http://blog.sciencenet.cn/u/zqq0ew0

博文

分享一个自己编写的工具-Matlab求小时均值

已有 3796 次阅读 2018-3-22 10:50 |个人分类:Matlab|系统分类:科研笔记

该代码是在小时均值的基础上增加的新功能。

  1. 分钟值会出现0值的问题,增加了0值是否参与计算 

  2. 自动计算开始和结束时间

  3. 时间必须是数字格式

基础代码是我师兄的。代码虽然简单,但很实用。希望对大家有帮助


function rhourly=avg_h(x,n_t,flag,to,te,t_shift)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 功能:小时均值统计 hourly means,包含0值统计和不统计,由flag控制

% format: 

%       rhourly=avg_h(x,n_t,flag,to,te,t_shift)

% % t_shift: e.g. 

% for i=1:n

% o=x(:,1)>to+t_shift+(i-1)/24&x(:,1)<=to+t_shift+i/24;

% ... 

% end

% 默认t_shift=1/60/24 (1分钟)

% Input Argument:

% x       (required) read data and column datetime in matrix must be time in form of Julian day

% n_t     (required) sequence number of column in datetime 

% flag    (optional) if true (default), x calculate include zeros value, if

%                    false, x calculate except zeros value

% to      (optional) the start datetime ( number format )

% te      (optional) the end datetime ( number format )

% t_shift (optional) the minute interval 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% ****note: column 1 in matrix must be time in form of Julian day

%-----------------------------------------------------------------------

if exist('n_t','var');

    %n_t=n_t; % sequence number of column in which tc (time of centre in an hour) data resides

else 

    error('必须指定n_t!');

end


if exist('to','var') && exist('te','var')

    to=floor(to); %

    te=ceil(te); %

else

    to=floor(min(x(:,1)));1.0;% Jan 1 in 2007,2008,2009 etc

    te=ceil(max(x(:,1)));

end


if to>=te

    error('to must be smaller than te in the fuction daily_means=avg_d(x,n_tc,to,te)!');

end


if exist('t_shift','var')

    %t_shift=t_shift; % 

else 

    t_shift=1/60/24;

end


if exist('flag','var')

    %t_shift=t_shift; % 

else 

    flag=true;

end

%-----------------------------------------------------------------------

n=ceil((te-to)*24);% 取整。te-to即te-1表示从2007-1-1 0:00开始

xm=zeros(n,length(x(1,:)));

xn=zeros(n,2);

[~,N]=size(x);

for i=1:n

    o=x(:,n_t)>to+t_shift+(i-1)/24 &x(:,n_t)<=to+t_shift+i/24;%找出每个小时内的数据 %每5分钟呢?o=r100s(:,1)>to+?

    io=find(o);

    xn(i,1)=length(io);%该小时内的有效数据条数

    xn(i,2)=to+(i-1)/24+0.5/24;%i=1时表示2007-1-1 00:30,即第一小时的中间。同理i=2时对应01:30。      

    if xn(i,1)>1 %若该小时内只有一条有效数据或没有

         if flag==true

             xm(i,:)=mean(x(io,:));%   

         else %只计算非0值

             for j=1:N

                 xtemp=x(io,j);

                 xm(i,j)=mean(xtemp(xtemp~=0));%

             end

         end

    elseif xn(i,1)==1

        xm(i,:)=x(io,:);%则将xm内第i小时的所有数据替换成-9999

    else

        xm(i,:)=-9999;

    end

end;

%

rhourly=[xm xn];

%-----------------------------------------------------------------------

o=rhourly(:,1)==-9999;

rhourly(o,:)=[];

for i=1:N-1

    rhourlytemp=rhourly(:,i+1);

    rhourlytemp(isnan(rhourlytemp)==1)=0;

    rhourly(:,i+1)=rhourlytemp;

end





https://blog.sciencenet.cn/blog-628499-1105150.html

上一篇:分享一个自己编写的工具-Matlab自动补全缺失日期
下一篇:Excel中如何让函数内的常数变量跟着行列数进行变化
收藏 IP: 114.242.25.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-24 12:19

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部