-1

I have the following code in matlab, which is extremely slow.this code is related to my previous post in Stack overflow I as wondering is there any way to make matlab faster, also when I run the code it should show me figure, with updated images, but it doesnt show any thing

%% 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...')`
Community
  • 1
  • 1
user261002
  • 2,182
  • 10
  • 46
  • 73
  • 3
    Wow, thats a lot of code! People on SO generally aren't inclined to work on questions that require making sense of several dozen lines of code, although there are exceptions. You'll get better responses if you post a few lines of code that you have identified as problematic, instead of posting a lot of code and asking people to find a nebulous bug/problem. Another benefit of posting short code segments is it allows other users to perform local test. There is no way that I can take what you've posted and run it locally, making it harder for me to help you. – slayton Aug 08 '12 at 20:00
  • 5
    A great tool for finding bottle necks in Matlab is to use the Profiler. It produces a nice report that tells where which lines of code are requiring the most time. I'd suggest you try it out. – slayton Aug 08 '12 at 20:02

1 Answers1

1

Well, I don't have 1000.mat, 1001.mat, ..., 4000.mat, so I can't really test this properly.

Anyway, what I can tell you offhand is

  • nested loops are generally a bad idea. Put most of your efforts into preventing the quadruple-nested loop you have

  • most loop bodies contain references to external, non-builtin functions (means2, int2str, ...). This means JIT compiling cannot be applied, and the for-loop runs at interpreter speed.

  • The contents of Yout and fImg changes size on each of the innermost iterations. Changing size should be avoided or at least kept to very small loops.

What it means to resize a variable, is that Matlab needs create a larger temp variable, copy the contents of the original array to the temp variable, append the new data to it, assign the temp variable to the original variable, and clean up the mess. As you can understand, all this pointless copying takes a lot of time when you have to do it lots of times (which you do), so: make an effort to preallocate your entire array.

Other than this we can't go without the mat files. As suggested by @slayton, learn to use the Matlab profiler. The points I've highlighted will probably jump out (as will the loading of the data, but well that can't be improved (unless ALL datafiles will fit in your memory?))

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96