vectorCounter

Iteraterates from start vector to finish vector.
7.8K Downloads
Updated 20 Feb 2009

No License

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

Cite As

michaelB brost (2024). vectorCounter (https://www.mathworks.com/matlabcentral/fileexchange/23047-vectorcounter), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Optimization Toolbox in Help Center and MATLAB Answers
Tags Add Tags
Acknowledgements

Inspired: DeleteZeros

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0