Be the first to rate this file! 16 downloads (last 30 days) File Size: 31.39 KB File ID: #26603

CounterInBase: Counting in arbitrary base

by Petr Pošík

 

05 Feb 2010 (Updated 08 Feb 2010)

Code covered by the BSD License  

Counter in arbitrary base with a convenient interface. The counter state can be saved to disc.

Download Now | Watch this File

File Information
Description

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.

c.next(), or
c.next(1)
   Returns the state of the counter and increments it by 1.

c.next(0)
   Returns the state of the counter and does not change its state.

c.next(20)
   Returns a matrix of the next 20 numbers and advances the state of the counter by 20.

c.save('filename')
   Saves the counter state to disc. If no filename is given, it uses the default name 'CounterInBaseState'.

c.load('filename')
   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.

Example:

  >> c = CounterInBase(3);
  >> c.next()
  ans =
       0
  >> c.next()
  ans =
       1
  >> c.next()
  ans =
       2
  >> c.next()
  ans =
       1 0
  >> c.next(3)
  ans =
       1 1
       1 2
       2 0
  >> c.next(4)
  ans =
       0 2 1
       0 2 2
       1 0 0
       1 0 1
  >> c.next(0)
  ans =
       1 0 2
  >> c.next(0)
  ans =
       1 0 2

MATLAB release MATLAB 7 (R14)
Other requirements Uses function handles.
Zip File Content  
Published M Files CounterInBase
Other Files CounterInBase.m,
html/demoCounterInBase.png,
html/demoCounterInBase_01.png,
html/demoCounterInBase_02.png,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Updates
08 Feb 2010

The original archive contained the .svn directory by mistake.

Tag Activity for this File
Tag Applied By Date/Time
counter Petr Pošík 08 Feb 2010 09:54:01
number conversion Petr Pošík 08 Feb 2010 09:54:01
number base Petr Pošík 08 Feb 2010 09:54:01
base conversion Petr Pošík 08 Feb 2010 09:54:01
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com