假设我有 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
最佳答案
一种可能的方法:
使用
accumarray
查找每年的最大值.
代码:
%// 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
,猜猜您会得到什么。 .比您的代码简单得多。
关于arrays - 从30年每日数据的庞大矩阵中选择最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25233316/