Be the first to rate this file! 26 downloads (last 30 days) File Size: 7.79 KB File ID: #23047

vectorCounter

by michaelB brost

 

20 Feb 2009

No BSD License  

Iteraterates from start vector to finish vector.

Download Now | Watch this File

File Information
Description

I don't know how many times I needed to rewrite this function. Its such a simple thing, hardly worth sharing, but now I won't have to rewrite it anymore.

I needed to iterate over a length-N vector from a start vector to a finish vector and at each iteration, check some conditions.

For example, a two-counter base 3 example would be
0 0
0 1
0 2
1 0
1 1
....etc...
2 1
2 2 <== Done

Here is the output from the demo mode of vectorCounter:

east significant count component is to the left
-----------------------------------------
start condition: -3 2 -1
end condition: 2 3 1
hit conditions: 2 3 5 7 10 12 30 33
pseudo code:
    do:
        [newVec, newCount, doneFlag, hitIndex] = vectorCounter(xEnd, xStart, hitList, oldVec, oldCount)
        oldVec = newVec
        oldCount = newCount

          do something here ....

    until(doneFlag == true)

-----------------------------------------
old count----old vector count-------hit index
    1 [ -3, 2, -1]
    2 [ -2, 2, -1] 1
    3 [ -1, 2, -1] 2
    4 [ 0, 2, -1]
    5 [ 1, 2, -1] 3
    6 [ 2, 2, -1]
    7 [ -3, 3, -1] 4
    8 [ -2, 3, -1]
    9 [ -1, 3, -1]
   10 [ 0, 3, -1] 5
   11 [ 1, 3, -1]
   12 [ 2, 3, -1] 6
   13 [ -3, 2, 0]
   14 [ -2, 2, 0]
   15 [ -1, 2, 0]
   16 [ 0, 2, 0]
   17 [ 1, 2, 0]
   18 [ 2, 2, 0]
   19 [ -3, 3, 0]
   20 [ -2, 3, 0]
   21 [ -1, 3, 0]
   22 [ 0, 3, 0]
   23 [ 1, 3, 0]
   24 [ 2, 3, 0]
   25 [ -3, 2, 1]
   26 [ -2, 2, 1]
   27 [ -1, 2, 1]
   28 [ 0, 2, 1]
   29 [ 1, 2, 1]
   30 [ 2, 2, 1] 7
   31 [ -3, 3, 1]
   32 [ -2, 3, 1]
   33 [ -1, 3, 1] 8
   34 [ 0, 3, 1]
   35 [ 1, 3, 1]
   36 [ 2, 3, 1] 0 <== DONE

Limitations: the iterators must step by 1 for each counter:
-3->-2->-1 is OK
-3->-1->-2 is NOT OK

As I noted, it is such a simple thing, it is hardly worth sharing. But...

michaelB brost Feb 2009

MATLAB release MATLAB 7.4 (R2007a)
Other requirements Native matlab code. Should run on any matlab implementation.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
23 Feb 2009 Jos (10584)

A few suggestions for improvement:
- check that all necessary output arguments are returned. For instance, this function has hardly any use when called with less than 4 output arguments
- make use of the function persistent, so that one can call the function once to initialize it, and then without arguments to update it, without having to go through all the checks and loops again.
- The coding can be improved: instead of the for-loop "% check end >= start" make use of ANY; "hitflag = ~isempty(hitIndex))" instead of the if-statement; etc ...

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
counter michaelB brost 20 Feb 2009 13:54:49
 

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