SAB*_*BER 2 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);
Run Code Online (Sandbox Code Playgroud)
手动完成并且我有相当少的相同数据集是很累人的.我用下面的代码计算了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
Run Code Online (Sandbox Code Playgroud)
一种可能的方法:
找到每年的最大值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
Run Code Online (Sandbox Code Playgroud)
作为奖励:如果您@max
在步骤2中更改任何一个@mean
或者@std
,请猜测您得到的...比您的代码简单得多.