arrays - 从30年每日数据的庞大矩阵中选择最大值

标签 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 

最佳答案

一种可能的方法:

  1. 使用 datenum 创建一个包含每个数据值年份的列。和 datevec照顾闰年。

  2. 使用 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/

相关文章:

c++ - C++无法返回已添加到 vector 列表中的所有变量

java - 试图实现选择排序但它不会工作

arrays - 计算向量中小于另一个向量中每个元素的值

c++ - 除非旋转为 0 度或 180 度,否则物体不会朝它所面对的方向移动

c++ - 获取当前的 ModelView 矩阵

python - 在postgresql中存储一个大矩阵并在python中操作它

python - 在 Python 中使用 rjust() 并减少 for 循环

matlab - 从大 mat 文件中读取变量

Matlab 编辑文本框 - 显示提示?

javascript - 如何在数组中添加数字