问题描述
我们有两个矩阵.将其中一个命名为"Date",并将另一个命名为"Data"日期矩阵中包含几列:
We have two matrices. Name one of them "Date", And another name is "Data"There are several columns in the Date matrix included:
year month day julusi hour
1951 1 1 1 0
1951 1 1 1 3
1951 1 1 1 6
1951 1 1 1 9
1951 1 1 1 12
1951 1 1 1 15
1951 1 1 1 18
1951 1 1 1 21
1951 1 2 2 0
1951 1 2 2 3
1951 1 2 2 6
1951 1 2 2 9
1951 1 2 2 12
1951 1 2 2 15
1951 1 2 2 18
1951 1 2 2 21
.... . . . .
.... . . . .
1951 12 30 364 0
1951 12 30 364 3
1951 12 30 364 6
1951 12 30 364 9
1951 12 30 364 12
1951 12 30 364 15
1951 12 30 364 18
1951 12 30 364 21
1951 12 31 365 0
1951 12 31 365 3
1951 12 31 365 6
1951 12 31 365 9
1951 12 31 365 12
1951 12 31 365 15
1951 12 31 365 18
1951 12 31 365 21
.... .. . .. .
2018 12 31 365 0
2018 12 31 365 3
2018 12 31 365 6
2018 12 31 365 9
2018 12 31 365 12
2018 12 31 365 15
2018 12 31 365 18
2018 12 31 365 21
在我的数据矩阵中,有410列(198696 * 410).我的日期矩阵的大小相等. "198696 * 1".我想将基于日期矩阵"的数据矩阵"转换为每日数据我使用以下代码
In my Data matrix, there are 410 columns(198696*410).The size of my Date matrices is equal. "198696*1". I want to convert the "Data Matrix on basis the Date Matrix to daily dataI use the following code
N=0;
for year=1951:2018;
for Juliusi=1:365;
cxa=(Date(:,4)==Juliusi);
cxb=(Date(:,1)==year);
a=cxa & cxb;
N=N+1;
dayy(N,:)=nanmean(Data(a,:));
end;end;
转换结果正确,但是矩阵大小不一样198696/8 = 24837是正确的,但是我的矩阵24820不正确问题出在哪儿?考虑leap日该怎么办?
The conversion result is correct, but the size of the matrix is not the same198696/8=24837 is correct but my matrix 24820 is incorrectWhere is the problem?What to do to consider leap days?
推荐答案
自从我最近从路易斯·门多(Luis Mendo)那里得知, ,我提出了以下想法:如果您的数据是完整的(即您可以保证,则总是有8个)您每天只需输入以下内容即可:
Since I recently learned from Luis Mendo, that convolution is the key to success, I came up with the following idea: If your data is complete, i.e. you can guarantee, that there are always 8 entries for each day, you can just simply use the following approach:
% Some test data.
Date = [
1951 1 1 1 0;
1951 1 1 1 3;
1951 1 1 1 6;
1951 1 1 1 9;
1951 1 1 1 12;
1951 1 1 1 15;
1951 1 1 1 18;
1951 1 1 1 21;
1952 1 2 2 0;
1952 1 2 2 3;
1952 1 2 2 6;
1952 1 2 2 9;
1952 1 2 2 12;
1952 1 2 2 15;
1952 1 2 2 18;
1952 1 2 2 21]
% Temporary result for convolution.
temp = conv2(Date, ones(8, 1)) / 8;
% Extract values of interest.
dayy = temp(8:8:end, :)
输出:
Date =
1951 1 1 1 0
1951 1 1 1 3
1951 1 1 1 6
1951 1 1 1 9
1951 1 1 1 12
1951 1 1 1 15
1951 1 1 1 18
1951 1 1 1 21
1952 1 2 2 0
1952 1 2 2 3
1952 1 2 2 6
1952 1 2 2 9
1952 1 2 2 12
1952 1 2 2 15
1952 1 2 2 18
1952 1 2 2 21
dayy =
1951.0000 1.0000 1.0000 1.0000 10.5000
1952.0000 1.0000 2.0000 2.0000 10.5000
如果您需要年份和日期信息,则可以分别获得这些信息.但是在您的原始帖子中,似乎不需要这些信息.
If you need the year and day information, then these could be obtained separately. But in your original post, these information seemed to be unneeded.
只需确保:我确实知道,我在示例中使用了Date
矩阵.但是,由于Date
遵循与Data
相同的格式,并且您可以轻松地验证所需的mean
操作的结果,因此我以它为例.
Just to be sure: I DO know, I used the Date
matrix in my example. But since, Date
follows the same format as Data
, and you can easily verify the results of the wanted mean
operation, I used it as an example.
这篇关于在Matlab中将小时数据转换为每日数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!