More efficient way for doing for and while loops.

조회 수: 25 (최근 30일)
Kranthidhar
Kranthidhar 2014년 3월 1일
댓글: Bee 2019년 1월 9일
Hi, I understand that in matlab for loops and while loops are some-what inefficient; I was wondering if there was an alternate, more efficient way of doing loops. This would apply to just basic for loop and while loop problems. Let me know if there is a simple way, but if there is an advanced method please also mention it.
-Thanks

답변 (2개)

Image Analyst
Image Analyst 2014년 3월 1일
for loops are not always bad. In fact there were cases with older versions of MATLAB where a for loop was faster than linear indexing (vectorizing). With the newer versions of MATLAB the for loops are a lot faster and can do millions of iterations in just a fraction of a second, so it's not always the for loop itself but how you are accessing the memory . For example with large arrays, for max speed, have the row loop be the inner loop and the column loop be the outer loop because it goes down memory in rows first, before it moves over to the next column.
  댓글 수: 1
Bee
Bee 2019년 1월 9일
That is a very important information, which I'll use wight now: row loops should be the inner loop. Thanks mate!!!

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


John D'Errico
John D'Errico 2014년 3월 1일
편집: John D'Errico 2014년 3월 1일
No, there is NOT an efficient way to do a for or while loops, at least not more efficient than for and while.
There are inefficient things you can do, however, that would take a book to cover every foolish thing you might try and tell you not to do those things. (As a hint, failing to preallocate arrays and vectors that you will grow dynamically is perhaps the worst thing I see done often.)
Learn to use the profile tool in MATLAB, and read MATLAB Answers. You will learn what to do and what not to do.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by