2017-04-25 128 views
0

我正在處理自上世紀80年代以來一直測量的10,000個變量的數據集。每個變量的第一次測量不是在同一天,變量是不規則測量的 - 有時測量僅相隔一個月,在相隔數十年的少數情況下。如何從線性插值數據獲得月度總數

我想在每個月的變量中獲得更改

到目前爲止,我有測量日期的細胞,和測量之間的變化的內插率(每個單元代表在單個變量,我只張貼每個陣列中的第一個5個細胞)

DateNumss= {[736614;736641;736669] [736636;736666] 736672 [736631;736659;736685] 736686} 

LinearInterpss={[17.7777777777778;20.7142857142857;0] [0.200000000000000;0] 0 [2.57142857142857;2.80769230769231;0]} 

如何獲得變量中插值變化的每月總和?

如果一個變量第一測量是由在1月1日,而之間的線性內插改變的下一個測量是每天1;下一次測量是在2月5日,相應的線性內插變化是2;那麼1月的總變化爲1 * 31(1天爲31天),而febuary總變化爲1 * 5 + 2 * 23(1天23天,2天爲5天)。

回答

0

您需要序列日期中與月份變化相對應的點。

mat(:,1)=sort(repmat(1980:1989,[1,12])); 
mat(:,2)=repmat(1:12,[1,size(mat,1)/12]); 
mat(:,3)=1; 
monthseps=datenum(mat); 

這給出了80年代所有120個月變化的列表。

現在你想每個月對每一天的變化進行總結。如果您採用原始數據,則更容易,因爲您可以使用matlab插入每天的值。如果您只有「LinearInterpss」,則需要使用Interp1與「previous」方法在日期中映射它。

for ct = 2:length(monthseps) 
    days = monthseps(ct-1):(monthseps(ct)-1); %days in the month 
    %now we need each day assigned a certain change. This value depends on your "LinearInterpss". interp1 with method 'previous' searches LineairInterpss for the last value. 
    vals = interp1(DateNumss,LinearInterpss,days,'previous'); 
    sum(vals); %the sum over the change in each day is the total change in a month 
end 
+0

不相當工作: '墊(:,1)排序=(repmat(2010:2019,[1,12])); mat(:,2)= repmat(1:12,[1,10]); mat(:,3)= 1; monthseps = datenum(mat); values = LinearInterpss {1}; datnums = DateNumss {1}; (值(datnums> monthseps(ct-1)和datnums Abijah

+0

我更新了答案以插入月份的所有日期。如果你輸入「help interp1」,你會發現你可以使用不同的插值方法,而不是「最近的」。 – Gelliant

+0

您的新方法會對插值進行求和;而不是他們之間的變化。我試圖獲得每個月***變化***爲每個變量。 – Abijah