从30年日常数据的巨大矩阵中选择最大值

时间:2014-08-10 21:45:29

标签: arrays matlab matrix

假设我在矩阵中有30年的每日数据。为简单起见,假设它只有1列,10957行表示30年的天数。这一年从2010年开始。我想找到每年的最大值,以便输出为1列和30行。有没有自动化的方法在Matlab中编程?目前我手动完成我所做的是:

%for the first year
  max(RAINFALL(1:365);
.
.
%for the 30th of year
  max(RAINFALL(10593:10957);

手动操作并且我的相同数据集很少。我用下面的代码计算了30年的平均值和标准差。我尝试修改代码以适用于上面的任务但我无法成功。希望任何人都可以修改代码或向我提出新的建议。

data = rand(32872,100); % replace with your data matrix

[nDays,nData] = size(data);

% let MATLAB construct the vector of dates and worry about things like leap
% year.
dayFirst = datenum(2010,1,1);

dayStamp = dayFirst:(dayFirst + nDays - 1);
dayVec = datevec(dayStamp);

year = dayVec(:,1);

uniqueYear = unique(year);

K = length(uniqueYear);

a = nan(1,K);
b = nan(1,K);

for k = 1:K
   % use logical indexing to pick out the year
   currentYear = year == uniqueYear(k);
   a(k) = mean2(data(currentYear,:));
   b(k) = std2(data(currentYear,:));
end 

2 个答案:

答案 0 :(得分:5)

一种可能的方法:

  1. 使用datenumdatevec创建一个包含每个数据值年份的列,以处理闰年。

  2. 使用accumarray找到每年的最大值。

  3. 代码:

    %// Example data: 
    RAINFALL = rand(10957,1); %// one column
    start_year = 2010;        %// data starts on January 1st of this year
    
    %// Computations:
    [year, ~] = datevec(datenum(start_year,1,1) + (0:size(RAINFALL,1)-1)); %// step 1
    result = accumarray(year.'-start_year+1, RAINFALL.', [], @max);        %// step 2
    

    作为奖励:如果您在步骤2中通过@max@mean更改@std,请猜猜您得到的内容......比您的代码简单得多。

答案 1 :(得分:1)

这可能会对您有所帮助:

RAINFALL = rand(1,10957); % - Your data here

firstYear = 2010;
numberOfYears = 4;
cum = 0; % - cumulative factor
yearlyData = zeros(1,numberOfYears); % - this isnt really necessary

for i = 1 : numberOfYears
    yearLength = datenum(firstYear+i,1,1) - datenum(firstYear + i - 1,1,1);
    yearlyData(i) = max(RAINFALL(1 + cum : yearLength + cum));
    cum = cum + yearLength;
end
相关问题