Calculate difference from a time series

10 ビュー (過去 30 日間)
Kostas
Kostas 2015 年 7 月 8 日
コメント済み: Kostas 2015 年 7 月 9 日
Hello all, i would appreciate your help to solve the following issue (avoiding unnecessary loops if possible). I have a time series of temperature with 4 daily values corresponding in times 0, 6, 12 and 18. I would like to create a new one where i will keep the day and the difference between the value temperature in 12 hr and 6 hr.
My dataset looks like
2007 1 1 0 5
2007 1 1 6 7
2007 1 1 12 14
2007 1 1 18 11
2007 1 2 0 8
2007 1 2 6 9
2007 1 2 12 12
2007 1 2 18 13
where first column is year, second month, third day, fourth time and sixth temperature. The new dataset i want to have will look like
2007 1 1 7
2007 1 2 3
Thank you in advance for your help

採用された回答

bio lim
bio lim 2015 年 7 月 8 日
編集済み: bio lim 2015 年 7 月 8 日
Hi. Clumsy code but should do the trick. Used vectorized code to avoid loops.
% I am assuming your dataset is a matrix
A = [2007 1 1 0 5;
2007 1 1 6 7;
2007 1 1 12 14;
2007 1 1 18 11;
2007 1 2 0 8;
2007 1 2 6 9;
2007 1 2 12 12;
2007 1 2 18 13 ];
% First thing you can do is select the rows of interest.
ii = 2:4:length(A);
jj = 3:4:length(A);
matrix = [A(ii.',:); A(jj.',:)];
% Remove fourth column
% matrix(:,2) = [];
% Sort row in ascending order
matrix = sortrows(matrix);
ll = 1:2:size(matrix, 1);
kk = 2:2:size(matrix, 1);
B = [matrix(kk.',:)];
C = [matrix(ll.',:)];
output = [B(:,1:end-2) B(:, end) - C(:, end)];
  1 件のコメント
Kostas
Kostas 2015 年 7 月 8 日
thank you very much coffee, it works!

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2015 年 7 月 8 日
I would do it like this:
temps = [
2007 1 1 0 5
2007 1 1 6 7
2007 1 1 12 14
2007 1 1 18 11
2007 1 2 0 8
2007 1 2 6 9
2007 1 2 12 12
2007 1 2 18 13];
[ymd, ~, idx] = unique(temps(:, 1:3), 'rows');
tempperday = nan(max(idx), 4);
tempperday(sub2ind(size(tempperday), idx, temps(:, 4)/6 + 1)) = temps(:, 5)
The tempperday array is pretty much the same data as temps but each row is a single day, and the columns are temperature at 0, 6, 8, 12 hours. Therefore to get the difference between 12 and 6:
diffperday = tempperday(:, 3) - tempperday(:, 2)
And to attach the day to it:
newtemps = [ymd diffperday]
  1 件のコメント
Kostas
Kostas 2015 年 7 月 9 日
dear Guillaume, thank you very much for your solution too!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by