Please help me vectorizing the following code

n=2:nt;
sum1(n)=f(t(n),alpha)+(var).*bj(n).*u(1);
k=1:n-1;var_bj=(bj(k+1)-bj(k)).*(var);
for n=2:nt
sum2=0;
for k=1:n-1
sum2=sum2+var_bj.*u(n-k);
end
sumout2(n)=sum2;
u(n)=(sum1(n)-sumout2(n))/(var-beta);
end

댓글 수: 1

Tell us more about the variables you have, for example, what are their sizes? What is your code trying to do?

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

 채택된 답변

Jan
Jan 2017년 3월 14일

0 개 추천

For a vectorization, move the index vector from the for loop into the command. For the innermost loop:
sum2 = 0;
for k = 1:n-1
sum2 = sum2 + var_bj .* u(n-k);
end
becomes (I guess that var_bj is a scalar):
sum2 = sum(u(n - (1:n-1))) * var_bj;
This can be simplified:
sum2 = sum(u(1:n)) * var_bj;

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2017년 3월 14일

답변:

Jan
2017년 3월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by