a question on for loop statement

Dear all,
I have this for loop
T=1000;
k=0.1;
u=rand(T,1);
a = zeros(T,1);
a(1) =u(1)+ k*0.01;
for t=2:T
a(t) = u(t,1) + k*a(t-1);
end
Is there a faster way of obtaining a? Maybe if I avoid loop?

댓글 수: 4

Luna
Luna 2019년 5월 16일
Is T variable very large?
madhan ravi
madhan ravi 2019년 5월 16일
Luna T is 1000
Adam Danz
Adam Danz 2019년 5월 16일
편집: Adam Danz 2019년 5월 16일
This is the tricky part: *a(t-1)
Short answer to "is there a faster way": Probably not.
There's probably a way to avoid the loop by replacing it with a convoluted, unreadable, jumble of functions but I doubt it will be as fast and it will not be as intuitive. If your loop works for you, keep it. It's simple, clean, and fast.
Luna
Luna 2019년 5월 16일
I agree with Adam I have tried with both T = 1000 and T = 1000000.
The time perfomances are below:
T = 1000 -> Elapsed time is 0.051244 seconds.
T = 1000000 -> Elapsed time is 0.073614 seconds.
The for loop is already as fast as it could be and the simplest solution.

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

답변 (1개)

Jos (10584)
Jos (10584) 2019년 5월 16일

3 개 추천

This is filtering.
T=10; % smaller example
k=0.1;
u=rand(T,1);
% your loop -> a
a = zeros(T,1);
a(1) =u(1)+ k*0.01; % i do not get this addition ...
for t=2:T
a(t) = u(t,1) + k*a(t-1);
end
% filtering -> aa
uu = u ;
uu(1) = uu(1) + k*0.01 ; % implement offset?
aa = filter(1, [1 -k], uu) ;
% do they produce the same result?
isequal(a, aa) % YES

댓글 수: 9

madhan ravi
madhan ravi 2019년 5월 16일
Answer using filter() was posted earlier but the OP claimed that it wasn't fast enough as the for loop.
Adam Danz
Adam Danz 2019년 5월 16일
Nice solution, Jos! I hadn't thought of that. It's quite fast and simple.
I ran both versions through a speed tests where each version was executed 100,000 times and then the median speeds were compared. The loop method is 1.6 times faster (p<0.001, Wilcox signed rank) on my machine with a difference of 0.004 milliseconds.
Jos (10584)
Jos (10584) 2019년 5월 16일
Thanks Adam :-) Did you exclude the copying of u and the add the initial (weird?) offset to u(1) directly? That would speed things up a little, I presume.
@madhan, sorry, I missed that post, apparently ....
Adam Danz
Adam Danz 2019년 5월 16일
편집: Adam Danz 2019년 5월 16일
Yea, the speed test was just between the loop and your single line that executes filter(). I'd bet that the filter() function uses a loop, too.
Jos (10584)
Jos (10584) 2019년 5월 16일
Internally filter uses a loop, for sure. I also expect error checks etc, that are omitted in the matlab for-loop. But still, it is pretty fast, and nice of course :-)
ektor
ektor 2019년 5월 16일
So, the filter is at least as fast as the for loop?
Adam Danz
Adam Danz 2019년 5월 16일
편집: Adam Danz 2019년 5월 16일
No, the loop is still faster. As Jos mentioned, there's overhead computations in filter() that aren't needed in your loop.
It's a MYTH that loops are always slower than alternatives.
If you want something neat and tidy, use Jos' solution. If you want something that will save you fractions of microseconds and something you already understand, use your loop.
Jos (10584)
Jos (10584) 2019년 5월 17일
btw, regarding execution time, you should also include the pre-allocation of the array :-D
Luna
Luna 2019년 5월 17일
+1 Jos :)

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2019년 5월 16일

댓글:

2019년 5월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by