问题描述
我有一个年度数据集:
Jday = datenum('2009-01-01 00:00','yyyy-mm-dd HH:MM'):1/24:...
datenum('2009-12-31 23:00','yyyy-mm-dd HH:MM');
DateV = datevec(Jday);
dat = 1+(10-1).*rand(length(Jday),10);
noise = 10*sin(2*pi*Jday/32)+20;
for i = 1:size(dat,2);
dat2(:,i) = dat(:,i)+noise';
end
表示通过水柱记录的温度测量值.我想计算每天06:00至18:00之间的温度范围,以便最终得到每个深度的365个值,因此最终矩阵为365x10.
which represents temperature measurements recorded through a water column. I would like to calculate the temperature range between 06:00 and 18:00 for each day so that I end up with 365 values for each depth so a final matrix of 365x10.
我可以通过以下方式指定各天:
I can specify the individual days by:
[~,~,b] = unique(DateV(:,2:3),'rows');
但是我不知道如何只考虑上午06:00和下午18:00之间记录的值.谁能提供一些有关最佳方法的信息?
But I cant work out how to only consider values recorded between 06:00 am and 18:00 pm. Could anyone provide some information about the best possible way of doing this?
修正:答案稍长
Jday = datenum('2009-01-01 00:00','yyyy-mm-dd HH:MM'):1/24:...
datenum('2009-12-31 23:00','yyyy-mm-dd HH:MM');
DateV = datevec(Jday);
dat = 1+(10-1).*rand(length(Jday),10);
noise = 10*sin(2*pi*Jday/32)+20;
for i = 1:size(dat,2);
dat2(:,i) = dat(:,i)+noise';
end
for i = 1:size(dat2,2);
data = [DateV(:,4),dat2(:,i)]; % obtain all hours from DateV array
dd = find(data(:,1) == 6); % find 06:00
dd(:,2) = find(data(:,1) == 18); % find 18:00
for ii = 1:size(dd,1);
result(ii,i) = range(data(dd(ii,1):dd(ii,2),2)); % calculate range
end
end
推荐答案
如果 range
带有矩阵,它在每一列上独立运行.我们可以使用它来准备一个矩阵,其中每列包含06:00到18:00时间间隔的值,并应用range
.
-
棘手的部分是处理365x24 x 10矩阵以使列正确:
The tricky part is manipulating your 365x24-by-10 matrix to get the columns right:
A = permute(reshape(dat2', [10, 24, 365]), [2, 1, 3]);
这应生成3-D 24x10x365矩阵,可以从中提取第7至18行.
This should result in 3-D 24x10x365 matrix, from which rows 7 to 18 can be extracted.
下一步是应用range
并将结果转换回二维365x10矩阵:
The next step is to apply range
and transform the result back into a 2-D 365x10 matrix:
result = permute(range(A(7:18, :, :)), [3, 2, 1]);
因此,完整的解决方案是:
Therefore, the full solution is:
A = permute(reshape(dat2', [10, 24, 365]), [2, 1, 3]);
result = permute(range(A(7:18, :, :)), [3, 2, 1]);
编辑:
为了使解决方案更加通用,可以使用DateV
获取所需行的索引:
EDIT:
To make the solution even more generic, you can use DateV
to obtain the indices of the desired rows:
idx = DateV(1:24, 4) >= 6 & DateV(1:24, 4) < 18;
,然后像这样在解决方案中使用它:
and then use it in the solution like so:
A = permute(reshape(dat2', [10, 24, 365]), [2, 1, 3]);
result = permute(range(A(idx, :, :)), [3, 2, 1]);
这篇关于在matlab中找到两个特定时间之间的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!