A simple pseudo-class (using function handles and nested functions) representing a counter in an arbitrary base. Useful e.g. for enumerating numbers in certain base.
c = CounterInBase(3)
Creates a new counter with base 3 and resets its state to 0.
Returns the state of the counter and increments it by 1.
Returns the state of the counter and does not change its state.
Returns a matrix of the next 20 numbers and advances the state of the counter by 20.
Saves the counter state to disc. If no filename is given, it uses the default name 'CounterInBaseState'.
Loads the previously stored counter state from disc. If no filename is given, it loads from file with the default name 'CounterInBaseState'.
NOTE: To be able to call the c.load() method, the counter must first exist: c.load() must be preceded by the call to c = CounterInBase(x), i.e. the c counter may be in any state with any base.
>> c = CounterInBase(3);
0 2 1
0 2 2
1 0 0
1 0 1
1 0 2
1 0 2
Petr Posik (2020). CounterInBase: Counting in arbitrary base (https://www.mathworks.com/matlabcentral/fileexchange/26603-counterinbase-counting-in-arbitrary-base), MATLAB Central File Exchange. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!