필터 지우기
필터 지우기

Pre Allocating for Speed

조회 수: 1 (최근 30일)
Nathan McClean
Nathan McClean 2019년 4월 4일
답변: Andreas Bernatzky 2019년 4월 4일
Hi,
My Matlab code comes up with an error regaring pre-allocating for speed. Would anybody be able to help me preallocate the following:
An example would be nice.
Thanks
%% Newmarks Method
a1=(1/(beta*dt^2))+((gamma/(beta*dt))*2*zeta*w_n);
a2=(1/(beta*dt))+(((gamma/beta)-1)*2*zeta*w_n);
a3=((1/(2*beta))-1)+((dt*2*zeta*w_n)*((gamma/(2*beta))-1));
wn_2=(w_n)^2+a1;
u_t(1)=0;
dudt(1)=0;
d2udt2(1)=0;
for n=1:length(Acc_g)-1
p_hat(n+1)=(Acc_g(n+1))+(a1*u_t(n))+(a2*dudt(n))+(a3*d2udt2(n));
u_t(n+1)=p_hat(n+1)/wn_2;
dudt(n+1)=((gamma/(beta*dt))*(u_t(n+1)-u_t(n)))+((1-(gamma/beta))*dudt(n))+(dt*(1-(gamma/(2*beta)))*d2udt2(n));
d2udt2(n+1)=((1/(beta*dt^2)))*(u_t(n+1)-u_t(n))-((1/(beta*dt))*dudt(n))-(((1/(2*beta))-1)*d2udt2(n));
end
  댓글 수: 2
Adam
Adam 2019년 4월 4일
편집: Adam 2019년 4월 4일
p_hat = zeros( 1, length(Acc_g) );
for...
...
end
etc.
although you should not use length ideally - either use numel for vectors or size, with the dimensions you want for larger dimension arrays.
Also, you will end up with a 0 at the beginning of each of your arrays (or you can pre-allocate NaNs if you prefer) since you only start from n+1 for some reason.
Also note it is presumably a warning, not an error that you are getting, and that from the M-Lint code analysis, not on running the code. Still, these warnings should be heeded so you are correct to do so!
John D'Errico
John D'Errico 2019년 4월 4일
+1 for a pretty accurate answer by Adam.

댓글을 달려면 로그인하십시오.

답변 (1개)

Andreas Bernatzky
Andreas Bernatzky 2019년 4월 4일
Hey Nathan,
if a vector is not preallocated it means that it grows in every Iteration.
This means you have to expand your vector, shift all the entries and shrink it back. Long Story short unnecessary memory usage. Here a small example:
%% Vector without preallocating
loops=20;
Vector1=[];
for(a=1:1:loops)
Vector1(end+1)=a;
end
%% Vector with preallocating
loops=20;
Vector2=ones(1,loops);
for(a=1:1:loops)
Vector2(a)=a;
end
Especially for your example preallocate your vector with ones or zero (depends on the application).
In your example i did it for p_hat.
%% Newmarks Method
a1=(1/(beta*dt^2))+((gamma/(beta*dt))*2*zeta*w_n);
a2=(1/(beta*dt))+(((gamma/beta)-1)*2*zeta*w_n);
a3=((1/(2*beta))-1)+((dt*2*zeta*w_n)*((gamma/(2*beta))-1));
wn_2=(w_n)^2+a1;
u_t(1)=0;
dudt(1)=0;
d2udt2(1)=0;
p_hat=ones(1,length(Acc_g)-1);
for n=1:length(Acc_g)-1
p_hat(n)=(Acc_g(n+1))+(a1*u_t(n))+(a2*dudt(n))+(a3*d2udt2(n));
u_t(n+1)=p_hat(n+1)/wn_2;
dudt(n+1)=((gamma/(beta*dt))*(u_t(n+1)-u_t(n)))+((1-(gamma/beta))*dudt(n))+(dt*(1-(gamma/(2*beta)))*d2udt2(n));
d2udt2(n+1)=((1/(beta*dt^2)))*(u_t(n+1)-u_t(n))-((1/(beta*dt))*dudt(n))-(((1/(2*beta))-1)*d2udt2(n));
end
Hope I could help.

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by