1. This solution is much faster on re-invocation than the one without the persistent num_ones variable. Unless of course it is performed on a much larger (than num_ones) array of 32-bit integer.
2. It is essential to have the statement
The reason for this is that the floor() function has a problem with precision. If can fail with 32-bit integer that are close to 2^32.
For instance, consider this Matlab code and system response:
Make an awesome ramp for a tiny motorcycle stuntman
Replicate elements in vectors
A different counting method
Genome Sequence 002: Introductory DNA Sequencing (Flipped Segments)
Mastermind II: Solve in 8 or less
Knot Count - Speed
GJam March 2016 IOW: Cody's Jams
Unique: Faster 'rows' for large array of uint8
Find the treasures in MATLAB Central and discover how the community can help you!
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office