You are now following this Submission
- You will see updates in your followed content feed
- You may receive emails, depending on your communication preferences
size() Vs. length() vs. stored array length when used in a for loop. This will ultimately show that the stored length is 17 times faster the using length() and 22 times faster than using size() in a for loop.
One must weigh the code savings by using size() and length() in a for loop. When only a few iterations are needed, size or length are inconsequential. For a large number of iterations, calculating the length of an array before entering a loop is preferred.
Cite As
Jason Nicholson (2026). size() Vs. length() vs. Storing Length Value Before Entering for Loop (https://www.mathworks.com/matlabcentral/fileexchange/48038-size-vs-length-vs-storing-length-value-before-entering-for-loop), MATLAB Central File Exchange. Retrieved .
General Information
- Version 1.1.0.0 (3.53 KB)
MATLAB Release Compatibility
- Compatible with any release
Platform Compatibility
- Windows
- macOS
- Linux
