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 :)

采纳的回答

Walter Roberson
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 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