Changeset 2218 in lmdz_wrf


Ignore:
Timestamp:
Nov 7, 2018, 4:43:47 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `all_consecutive_combs': Function to provide all the consecutive possible combinations from a series of indices
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2216 r2218  
    5959# advance_date: Function to advance a date object a certain given units of time
    6060# advance_matDate: Function to advance matrix-date with a matrix-increment
     61# all_consecutive_combs: Function to provide all the consecutive possible
     62#   combinations from a series of indices
    6163# angle_DegMinSec: Function to transform an angle to Degrees Minutes Seconds
    6264# ASCIIfile_stats: Function to provide the statistics of a series of values from an ASCII file
     
    1472714729    return colors
    1472814730
     14731def all_consecutive_combs(sizes):
     14732    """ Function to provide all the consecutive possible combinations from a series
     14733      of indices
     14734      sizes: list of indices to expand and combine
     14735    >>> all_consecutive_combs([3, 2, 2])
     14736    [[0 0 0]
     14737     [0 0 1]
     14738     [0 1 0]
     14739     [0 1 1]
     14740     [1 0 0]
     14741     [1 0 1]
     14742     [1 1 0]
     14743     [1 1 1]
     14744     [2 0 0]
     14745     [2 0 1]
     14746     [2 1 0]
     14747     [2 1 1]]
     14748    """
     14749    fname = 'all_consecutive_combs'
     14750
     14751    Nsizes = len(sizes)
     14752    Ncombs = np.prod(sizes)
     14753    combs = np.zeros((Ncombs, Nsizes), dtype=int)
     14754
     14755    itervals = np.zeros((Nsizes), dtype=int)
     14756    for icomb in range(Ncombs):
     14757        combs[icomb,:] = itervals
     14758        overlimit = True
     14759        for i in range(Nsizes):
     14760            ind = Nsizes-i-1
     14761            itervals[ind] = itervals[ind] + 1
     14762            if itervals[ind] < sizes[ind]: overlimit = False
     14763            if not overlimit:
     14764                break
     14765            else:
     14766                itervals[ind] = 0
     14767
     14768    return combs
     14769
     14770#print all_consecutive_combs([3, 2, 2])
     14771
    1472914772#quit()
    1473014773
Note: See TracChangeset for help on using the changeset viewer.