2012-08-08 46 views
-1

我在matlab中有以下代碼,這是非常slow.this代碼是關係到我的previous post堆棧溢出我想知道有什麼辦法讓matlab更快,也當我運行它應該表現出我的身影,用更新的圖像的代碼,但它不顯示任何東西試圖優化一些代碼,並使其更快

%% Loading Data 
cd('D:\MatlabTest\15-07SpeedSensitivity\0.3'); 
clear all 
row=101; 
column=311; 

%% 
%Coefficients Creation 
N=5; 
W = [0.005 0.10;0.10 0.20;0.20 0.30;0.30 0.40;0.40 0.50;0.50 0.60 ;0.60 0.70;0.70 0.80 ;0.80 0.90;0.90 1.0]; 
for ind=1:9 
    wn = W(ind,:); 
    [b,a] = butter(N,wn); 
    bCoeff{ind}=b; 
    aCoeff{ind}=a; 
end 
[bCoeff{10},aCoeff{10}]=butter(N,0.9,'high'); 

%% 
%filter initialization 
ZState = cell(1,10); 
for i=1:10 
    ZState{i} = zeros(max(length(aCoeff{i}), length(aCoeff{i})) - 1, 1); %# This is the initial filter state 
end 
%% 
bands=10; 
for b=1:bands 
    Yout{b}{row, column}=[]; 
end 

%% 
j=1; 
K = 1000:4000; 
window = zeros(1,10); 
figure; 
y = 0;   %# Preallocate memory for output 
j=0; 
buffSize=10; 
tempMean{row,column}=[]; 
Gibbs=(length(K)*3)/100; 
fImg{1}(row,column)=0; 
%load one image 
for i = 1000:length(K) 
    disp(i) 
    str = int2str(i); 
    str1 = strcat(str,'.mat'); 
    load(str1); 
    D(:,:) = A(100:200 ,200:510); 
    %go throught the columns and rows 
    for p = 1:row 
     for q = 1:column 
      %calculte the temporal mean value based on previous ones 
      if(size(tempMean{p,q})<buffSize) %init the first 10 
       tempMean{p,q}=[D(p,q) tempMean{p,q}]; 
      else 
       tempMean{p,q}=[D(p,q) tempMean{p,q}(1:end-1)]; 
      end 
      if(mean2(tempMean{p,q})==0) 
       x=0; 
      else 
       x = double(D(p,q)/mean2(tempMean{p,q})); 
      end 
      %filtering for 10 bands, based on the previous state 
      for f = 1:10 
       [y, ZState{f}] = filter(bCoeff{f},aCoeff{f},x,ZState{f}); 
       if(j<Gibbs) 
        continue; 
       end 
       if(size(Yout{f}{p,q})<10)%init the first 10 after Gibbs phenomenon 
        Yout{f}{p,q} = [y.^2 Yout{f}{p,q}]; 
       else 
        Yout{f}{p,q} = [y.^2 Yout{f}{p,q}(1:end-1)]; 
        fImg{f}(p,q)=mean2(Yout{f}{p,q}); 
       end 
      end 
     end 
    end 
    if(size(fImg{1}(1,1))>1) 
    for k = 1:10 
     subplot(5,2,1); 
     subimage(fImg{k}*5000, [0 0.5]); 
     colormap jet 
    end 
    pause(0.01); 
    end 
    j=j+1; 
end 
disp('Done Loading...')` 
+3

哇,這就是很多碼! SO上的人們通常不傾向於需要理解幾十行代碼的問題,儘管也有例外。如果您發佈了幾行已確定爲有問題的代碼,而不是發佈大量代碼並要求人們查找模糊的錯誤/問題,則會得到更好的回覆。發佈短代碼段的另一個好處是它允許其他用戶執行本地測試。我無法接受你發佈的內容並在本地運行,這讓我很難幫助你。 – slayton 2012-08-08 20:00:39

+5

在Matlab中查找瓶頸的一個好工具是使用Profiler。它產生一個很好的報告,告訴哪些代碼行需要最多的時間。我建議你試試看。 – slayton 2012-08-08 20:02:05

回答

1

好了,我沒有1000.mat1001.mat,...,4000.mat,所以我真的不能測試這適當。

不管怎樣,我可以告訴你的副手是

  • 嵌套循環一般是一個壞主意。將你的大部分努力放在防止你擁有四重循環的循環上。

  • 大多數循環體包含對外部非內建函數(means2,int2str,...)的引用。這意味着無法應用JIT編譯,並且for -loop以解釋器速度運行。

  • YoutfImg的內容會改變每個最內層迭代的大小。應該避免更改大小或至少保持非常小的循環。

意味着什麼來調整的變量,是Matlab的需求造成了較大的臨時變量,原始數組的內容複製到臨時變量,新數據追加到它,臨時變量分配給原始變量,並清理混亂。如你所知,所有這些毫無意義的複製都需要很多時間,因此你必須多次執行這些操作(你這麼做),所以:努力預先分配整個數組

除此之外,我們不能沒有mat文件。正如@slayton所建議的,學會使用Matlab profiler。我已經突出顯示的點可能會跳出(數據加載,但無法改善(除非所有數據文件將適合您的內存?))