2013-08-24 43 views
0

我寫了一小段代碼來同步兩個財務時間序列。我下載了一些外匯數據,還有一些缺失的交易。這裏的理念是獲得最大的一套,並與其他人同步。如何完全向量化這個?

例如,我有一組這樣的

a= [20010110 2310 10; 
     20010110 2311 20; 
     20010110 2313 30] 
    b= [20010110 2309 50; 
    20010110 2312 52] 

,我想,然後我得到這個

c =[20010110 2310 50;; 
     20010110 2311 50 
     20010110 2313 52] 

c是幾乎同樣的事情作爲,但這只是一個指數。

,所以我寫這篇

function [setAjustado] = ajustar(SetCorreto,SetParaAjustar) 

    dataCorreto = SetCorreto(:,1); % get the date from the correct set 
    dataAjustar = SetParaAjustar(:,1); % get the date from the set to be corrected 
    minCorreto = SetCorreto(:,2); % get the timeframe from the correct set 
    minAjustar = SetParaAjustar(:,2);get the timeframe from the set to be corrected 
    setAjustado = zeros(size(SetCorreto)); %corrected set 
    idxI = dataAjustar == dataCorreto(1); %generating the first range to search 

    for i=2:size(SetCorreto,1) 
    try 

     if (i >1 && dataCorreto(i) ~= dataCorreto(i-1)) % if the dates are the same, i dont need to look for the range again 
     idxI = dataAjustar == dataCorreto(i); % generate the range to search 
     idxIa = find(idxI==1,1); % find the first index 
     end 

     idx = find(minAjustar(idxI)>=minCorreto(i),1) +idxIa; % find the nearest occurency in the set to be corrected to match the correct set 
     setAjustado(i,:) = SetParaAjustar(idx,:); %replace all the line. This line have prices close, max, low and open. 
     setAjustado(i,2) = minCorreto(i); %adjust the timeframe to match the correct set 
    catch 
     if i==1 % in case of i to be greater then the size of set to be corrected 
      a=i; 
     else 
      a= i-1; 
     end 
    setAjustado(i,:) = setAjustado(a,:); % will copy the last line created in corrected set 

     end 
    end 

但是我想這個東西是很慢...有人可以幫助我加快這件事?

Tks提前!

+0

我不明白「syncrhonise」是什麼意思。你想要做什麼計算? –

回答

1

根據您發佈的數據與您的意見,這裏是我的嘗試:

% first two columns are considered "keys", last one contains the values 
a = [20010110 2310 10; 
    20010110 2311 20; 
    20010110 2313 30]; 
b = [20010110 2309 50; 
    20010110 2312 52]; 

% get a row identifier for each instance 
[~,~,ind] = unique([a(:,1:2);b(:,1:2)], 'rows'); 
ind_a = ind(1:size(a,1)); 
ind_b = ind(size(a,1)+1:end); 

% merge the data 
c = nan(max(ind),size(a,2)); 
c(ind_a,1:end-1) = a(:,1:end-1); 
c(ind_b,:) = b; 

% fill-in missing values from last know values 
for i=2:size(c,1) 
    if isnan(c(i,end)) 
     c(i,end) = c(i-1,end); 
    end 
end 

% only keep instances matching A's rows 
c = c(ind_a,:); 

結果:

>> c 
c = 
    20010110  2310   50 
    20010110  2311   50 
    20010110  2313   52 

如果實際數據中包含多個列,你需要相應地調整代碼。

+0

Amro,我做了一點修改,一切都像魅力一樣工作 非常感謝! %合併數據 c = nan(max(ind),size(a,2)); (ind_a,1:2)= a(:,1:2); c(ind_b,:) = b; (i,3)結束= c(i,1) 如果isnan(c(i,end)) c 3:端);結束 結束 –