开发者

Improving runtime in Matlab?

I have some code that is taking a long time to run(several hours) and I think it is because it is doing a lot of comparisons in the if statement. I would like it to run faster, does anyone have any helpful suggestions to improve the runtime? If anyone has a different idea of what is slowing the code down so I could try and fix that it would be appreciated.

xPI = zeros(1,1783);
argList2 = zeros(1,1783);
aspList2 = zeros(1,1783);
cysList2 = zeros(1,1783);
gluList2 = zeros(1,1783);
hisList2 = zeros(1,1783);
lysList2 = zeros(1,1783);
tyrList2 = zeros(1,1783);

minList= xlsread('20110627.xls','CM19:CM25');
maxList= xlsread('20110627.xls','CN19:CN25');
N = length(pIList);
for i = 1:N
    if (argList(i)>= minList(1) && argList(i) <= maxList(1)) ...
        && (aspList(i)>= minList(2) && aspList(i) <= maxList(2))开发者_如何转开发 ...
        && (cysList(i)>= minList(3) && cysList(i) <= maxList(3)) ...
        && (gluList(i)>= minList(4) && gluList(i) <= maxList(4)) ...
        && (hisList(i)>= minList(5) && hisList(i) <= maxList(5)) ...
        && (lysList(i)>= minList(6) && lysList(i) <= maxList(6)) ...
        && (tyrList(i)>= minList(7) && tyrList(i) <= maxList(7))

        xPI(i) = pIList(i);
        argList2(i) = argList(i);
        aspList2(i) = aspList(i);
        cysList2(i) = cysList(i);
        gluList2(i) = gluList(i);
        hisList2(i) = hisList(i);
        lysList2(i) = lysList(i);
        tyrList2(i) = tyrList(i);
        disp('passed test');
    end
end


You can try vectorising the code; I have made up some sample data sets and duplicated some of the operations you're performing below.

matA1 = floor(rand(10)*1000); 
matB1 = floor(rand(10)*1000);

matA2 = zeros(10); 
matB2 = zeros(10);

minList = [10, 20]; 
maxList = [100, 200];

indicesToCopy = ( matA1 >= minList(1) ) & ( matA1 <= maxList(1) ) & ( matB1 >= minList(2) ) & ( matB1 <= maxList(2) );

matA2(indicesToCopy) = matA1(indicesToCopy); 
matB2(indicesToCopy) = matB1(indicesToCopy);

No idea whether this is any faster, you'll have to try it out.

EDIT:
This doesn't matter too much since you're only making two calls, but xlsread is horribly slow. You can speed up those calls by using this variant syntax of the function.

num = xlsread(filename, sheet, 'range', 'basic')

The catch is that the range argument is ignored and the entire sheet is read, so you'll have to mess with indexing the result correctly.


Use the profiler to see which lines or functions are using the most execution time.

You can probably get a huge increase in execution speed by vectorizing your code. This means using operations which operate on an entire vector at once, instead of using a for-loop to iterate through it. Something like:

% make a logical vector indicating what you want to include
ii = (argList >= minList(1) & argList  <= maxList(1)) & ...

% use it
argList2(ii) = arglist(ii); % copies over every element where the corresponding ii is 1
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜