Update odometer

From QETLAB
Jump to navigation Jump to search
update_odometer
Increases a vector subject to limits on how large each entry can be

Other toolboxes required none
Function category Helper functions
This is a helper function that only exists to aid other functions in QETLAB. If you are an end-user of QETLAB, you likely will never have a reason to use this function.

update_odometer is a function that implements an "odometer": that is, it increases the right-most entry of a vector by 1. If that right-most entry is now larger than its maximum allowed value then it "rolls over" to 0 and the second-to-right-most entry increases by 1. Similarly, if this entry is now too larger, it rolls over, and so on.

This function is useful if you are in a situation where you want to nest multiple for loops, but you don't know beforehand exactly how many of them there will be (since it depends on user input, for example).

Syntax

  • NEW_IND = update_odometer(OLD_IND,UPPER_LIM)

Argument descriptions

  • OLD_IND: The vector to be updated.
  • UPPER_LIM: A vector of the same length as OLD_IND with the property that its j-th entry specifies the "roll over" value of the j-th entry of OLD_IND. That is, the j-th value of OLD_IND is constrained to being in the interval from 0 to UPPER_LIM(j)-1 (inclusive).

Examples

The following code loops over all vectors of length 2 with the property that their first entry is 0, 1, or 2, and their second entry is 0 or 1:

>> v = [0,0]; upper_lim = [3,2];
>> for j = 0:prod(upper_lim)-1
     v = update_odometer(v,upper_lim)
   end

v =

     0     1


v =

     1     0


v =

     1     1


v =

     2     0


v =

     2     1


v =

     0     0

Source code

Click on "expand" to the right to view the MATLAB source code for this function.