Effacer les filtres
Effacer les filtres

Calculate difference from a time series

5 vues (au cours des 30 derniers jours)
Kostas
Kostas le 8 Juil 2015
Commenté : Kostas le 9 Juil 2015
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

Réponse acceptée

bio lim
bio lim le 8 Juil 2015
Modifié(e) : bio lim le 8 Juil 2015
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 commentaire
Kostas
Kostas le 8 Juil 2015
thank you very much coffee, it works!

Connectez-vous pour commenter.

Plus de réponses (1)

Guillaume
Guillaume le 8 Juil 2015
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 commentaire
Kostas
Kostas le 9 Juil 2015
dear Guillaume, thank you very much for your solution too!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Time Series dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by