필터 지우기
필터 지우기

Vectorization of a for loop

조회 수: 1 (최근 30일)
David Valle
David Valle 2020년 3월 19일
댓글: David Valle 2020년 3월 23일
Hello,
I have to do a large calculation and the next foor loop takes like 99% of runtime in the script:
for n = 1:iteraciones-1
x(n+1) = a - x(n)^2 + b*y(n);
y(n+1) = x(n);
end
I've read that if I manage to vectorize it, it will improve my code performance, but im stuck with
x(1) = 1;
y(1) = 1;
x(2) = a - x(1)^2 + b*x(1);
for n = 2:iteraciones-1
x(n+1) = a - x(n)^2 + b*x(n-1);
end
y(2:iteraciones) = x(1:iteraciones-1);
And I cant think of a way to vectorize that for loop since it uses previous generated values
If someone could take the time to figure this out or link a good resource on how to vectorize, it would be much apreciated
Thanks in advice :)
  댓글 수: 1
darova
darova 2020년 3월 19일
I think your code can't be vectorized. As your value of x(i+1) depends on previous x(i)

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

채택된 답변

Aditya Patil
Aditya Patil 2020년 3월 23일
I understand that you want to vectorize the for loop to improve performance.
However vectorization of for loops requires that each iteration is independent of each other. This is not the case in your code, as x(n + 1) depends upon x(n). Hence I think it is not possible to vectorize this code.
  댓글 수: 1
David Valle
David Valle 2020년 3월 23일
Alright, what a pity.
Anyways, thank you for your answer :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by