Calculate difference from a time series
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
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
댓글 수: 0
채택된 답변
  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개)
  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]
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

