필터 지우기
필터 지우기

Very slow for loop

조회 수: 4 (최근 30일)
Rafael
Rafael 2011년 12월 31일
Hi, I am new to Matlab, i have more experience with C/C++
I am trying run this simple loop:
x=zeros([10001,10001,2]);
for t=1:2
for z =1:10000
for i =1:10000
x(z,i,t)=i+z+t+2;
end
end
end
x(5,5,1)
But is taking a while..imagine many of then inside a single algorithm. in C++ this loop runs in a few seconds...
Is there any other way to run this loop in Matlab faster???
Sorry if my question is silly, first time running Matlab. I tried some vectorization but for nested loops things get really complicated, especially with different loop sizes.
Thanks
  댓글 수: 3
Daniel Shub
Daniel Shub 2011년 12월 31일
Why should that be any faster/slower than 1:10000? The variable x is already intialized to a size that handles either case.
Andrew Newell
Andrew Newell 2011년 12월 31일
The point of my question is that the values in each row and column increase until they suddenly drop to zero at the end. I'm guessing that is not what Rafael intended.

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

채택된 답변

Daniel Shub
Daniel Shub 2011년 12월 31일
You might be able to get a slight performance boost by changing how the memory is acessed ...
Basically x(z,i,t) might not be the same as x(t,z,i) or x(i,z,t).
  댓글 수: 1
Rafael
Rafael 2011년 12월 31일
Hi Daniel, Thanks!!! Indeed improved the permornce by 2-4 seconds but is still very slow, almost 25 seconds, on my machine (not a powerful one though). Running it using C++ on GCC 4.6 (with architecture and performance flags optimized) it runs in 3 seconds. The difference is huge. Thanks anyway and happy new year (not yet in Brazil - almost 5 hours to go)!!!!

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

추가 답변 (3개)

the cyclist
the cyclist 2012년 1월 1일
x = bsxfun(@plus,bsxfun(@plus,1:10000,(1:10000)'),permute(1:2,[1 3 2]))+2;
  댓글 수: 1
Rafael
Rafael 2012년 1월 1일
Thanks!, indeed much faster!

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


Andrew Newell
Andrew Newell 2011년 12월 31일
For a problem where the numbers count upward monotonically in each direction, here is a compact and fast solution:
y = repmat(2:10002,10001,1);
x = cat(3,y+y',y+y'+1);
On my computer, your code takes about 25 seconds and mine takes 6 seconds.
  댓글 수: 2
Rafael
Rafael 2011년 12월 31일
Hi, thanks.
As I said before, according to your feedback I have to avoid for loops in my code. Will be hard to translate C++ codes for Matlab if this is true.
Andrew Newell
Andrew Newell 2012년 1월 1일
I think that you mostly can leave loops in because MATLAB uses just-in-time compiling on loops. However, as far as I know this compiling isn't done on arrays of dimension greater than 2.

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


Jan
Jan 2012년 1월 1일
If the shown code is your original problem, and not a simplification, I'm in doubt, that it is an efficient approach: You occupy 1.6GB memory with values, which are very easy to calculate dynamically.
Note: The reference "x(z,i,t)" requires two multiplications, while "i+z+t+2" uses 3 additions only.
  댓글 수: 1
Rafael
Rafael 2012년 1월 1일
You are right. The intention is to make a n-by-n-2 array.

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

카테고리

Help CenterFile Exchange에서 Waveform Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by