Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 675
Ben Fossen Avatar asked Feb 21 '26 10:02

Ben Fossen


2 Answers

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.

like image 153
Praetorian Avatar answered Feb 24 '26 08:02

Praetorian


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
...
like image 32
nibot Avatar answered Feb 24 '26 08:02

nibot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!