Matlab getting progressively slower in running simulation

2 ビュー (過去 30 日間)
Nagesh A P
Nagesh A P 2018 年 6 月 15 日
コメント済み: Stephen23 2018 年 6 月 15 日
The matlab code I'm running gets slower as time moves on. It's in this part of my program, which has a loop. the loop contains an isnan() function and some if else statements. The loop is used to concatenate values to a matrix using the cat function. The loop has to iterate around 160,000 times. The loop starts off fast, iterating 30000 times in aroun 3 seconds and the rest 130,000 is done in aroun 2 minutes. Just want to know why it's so.

採用された回答

Stephen23
Stephen23 2018 年 6 月 15 日
編集済み: Stephen23 2018 年 6 月 15 日
Because this: "The loop is used to concatenate values to a matrix using the cat function" forces MATLAB to move the array in memory on each loop iteration, which is very slow as the array gets larger. This topic has been discussed thousands of times on this forum, so you can use the search feature (top of this page) to find explanations of why this happens, and you should read the documentation too:
  2 件のコメント
Nagesh A P
Nagesh A P 2018 年 6 月 15 日
Didn't know that would help this much, I'm new to matlab. Thanks for the help.
Stephen23
Stephen23 2018 年 6 月 15 日
@Nagesh A P: it is recommended to preallocate all arrays before loops, just as a matter of habit.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by