2013-03-01 80 views
0

我有一個非常大的矩陣(750000 x 6),其中我有一個股票價格的時間列。時間間隔不統一,因此我插入了新的時間。由於我只想在工作日的工作時間內包含時間,所以我有一個for循環,用於檢查插值時間是否在該範圍內,並將這些條目複製到新矩陣中。但是,我的程序需要永久運行(> 10分鐘)。這是我的原代碼:爲MATLAB中的大矩陣優化循環

A = IBM; 
times = A(:,1); 
incr = t; 
uniqday = datevec(times); 
uniqday = unique(uniqday(:,1:3), 'rows'); % unique days for data 

% Computes interpolated prices at sampling interval 
interptimes = (times(1):incr:times(end)).';  

% Fractional hours into the day 
frac_hours = 24*(interptimes - floor(interptimes));  
% Exclude interpolated times outside of trading hours 
newtimes = interptimes((frac_hours > 0) & (frac_hours < 19.1)); 

% Exclude weekends & holidays 
newtimesvec = datevec(newtimes); 
newtimesvec2 = zeros(length(newtimesvec),6); 
for j = 1:length(uniqday) 
    for i = 1:length(newtimesvec) 
     if newtimesvec(i,1:3) == uniqday(j,:) 
       newtimesvec2(i,:) = newtimesvec(i,:); 
     else  %Program always get stuck at this line 
     end 
    end 
end 

所以我想,也許刪除條目插的時間陣列中會更快,而不是複製到一個新的矩陣,但是當一個條目被刪除,內插的時間序列改變大小,所以這個for循環會導致它訪問矩陣之外。我不知道如何解決這個問題。

for i = 1:length(interptimes) 
    for j = 1:length(uniqday) 
     if interptimes(j,1:3) == uniqday(j,:) 
     else 
      interptimes(j,:) = []; 
     end 
    end 
end 

回答

0

你可以只使用ismember地發現,在uniqday的日子:

newtimesvec2 = newtimesvec(ismember(newtimesvec(:,1:3),uniqday,'rows'),:);