Effacer les filtres
Effacer les filtres

Is there a method of significant preallocation beyond zeros?

2 vues (au cours des 30 derniers jours)
Kelly Catlin
Kelly Catlin le 24 Mar 2018
Commenté : John D'Errico le 24 Mar 2018
I have been calling a function with 83000 iterations (as below):
>>[t1data,t2data,Tdata,Ddata,lamdata] = example_run(2,0.1,25,1.01)
Yet I have let it run for the better part of a day without the program terminating. Is there a way (beyond preallocation with zeros, as I have already done) to decrease the run time significantly?
function [t1data,t2data,Tdata,Ddata,lamdata] = example_run(E0,sigma,F,tau)
%E0=2;sigma=0.1;F=25;tau=1.01; standard reference
%preallocation
Tdata = zeros(1, 100000);
t1data = zeros(1, 100000);
t2data = zeros(1, 100000);
kdata = zeros(1, 100000);
Ddata = zeros(1, 100000);
lamdata = zeros(1, 100000);
[Tc,Ts]=findTcTs(E0,sigma,F,tau);
%T=Ts+0.5;
%[t1,t2,lambda,D]=findt1t2lambdaD(E0,sigma,F,tau,T,Tc);
hold on;
T = Ts;
T0 = Tc;
for k = 1:83001
[t1,t2,lambda,D] = findt1t2lambdaD(E0,sigma,F,tau,T,T0);
Tdata = [Tdata,T];
t1data = [t1data,t1];
t2data = [t2data,t2];
kdata = [kdata,k];
Ddata = [Ddata,D];
lamdata = [lamdata, lambda];
T0 = t2;
T = T + 0.1;
plot(T,real(D), 'o')
%iterate by increments to use valid initial guess for t2 for each increment in findt1t2...
end
hold off;

Réponse acceptée

Walter Roberson
Walter Roberson le 24 Mar 2018
Tdata = [Tdata,T];
Don't do that. You already used
Tdata = zeros(1, 100000);
so each iteration you are expanding Tdata by putting a single new element after all of the zeros. That is not preallocation! Preallocation would be if you had used
Tdata = zeros(1, 100000);
with
Tdata(k) = T;
(It is not obvious why your 100000 does not match your 83001)
  1 commentaire
John D'Errico
John D'Errico le 24 Mar 2018
Actually, what was done was worse than not preallocating at all.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Geographic Plots dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by