How can i make my integral more efficient?
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (1)
Are Mjaavatten
le 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 commentaire
Mads Schnoor Nielsen
le 22 Mar 2018
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!