Efficiently use length of array / vector / matrix
5 ビュー (過去 30 日間)
古いコメントを表示
I have a script that references the lengths of a few VERY long "1 by x" arrays in a few spots. Would it be more efficient to store the lengths of these arrays as variables prior to their reference?
I am currently doing lines like this:
for i = 1:1: (length(s)) % Where s is the array ....
I am not sure if Matlab forgoes recalculating the length of an array if it notices it does not change since the last time it was calculated... Thank you in advance and I apologize for my coding ignorance :)
0 件のコメント
採用された回答
Walter Roberson
2016 年 6 月 14 日
My expectation would be that it will re-check the size of the array so it would be faster to store it. This is something you could benchmark. Note that for loops calculate the endpoints once and store the endpoints internally as constants, so any cost for doing the size() is going to be amoratized over the loop.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!