How can i make my integral more efficient?
Show older comments
I am doing an integral in the function p. To save computational time, I have made the function consisting of N discrete datapoints, and then finding the sum from point x to y:
ix1 = find(p <=dataset2(d+k,9) , 1, 'last');
ix2 = find(p >= dataset2(d+k,12), 1, 'first');
dataset2(d+k,10) = alpha(i)*dataset2(d+k,5)+sum(p(ix2:ix1)); % only the sum after the + is important here.
This gives me the exact results I need, however, since I need to run my code, and calibrate it to fit data, I need it to be faster. These three lines accounts for 87% of the runtime of a 120 lines code. Any help would be appreciated! And please ask if I am not specific enough.
Accepted Answer
More Answers (1)
Are Mjaavatten
on 22 Mar 2018
Using logical indexing instead of find will be faster:
relevant = p >= dataset2(d+k,9) & p <= dataset2(d+k,12);
dataset2(d+k,10) = alpha(i)*dataset2(d+k,5)+sum(p(relevant));
1 Comment
Mads Schnoor Nielsen
on 22 Mar 2018
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!