Vec partitions

From QETLAB
Jump to navigation Jump to search
vec_partitions
Produces all possible partitions of a vector

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.

vec_partitions is a function that returns all partitions a given vector into a specified number of parts. Note that, for this function, the order of the parts does matter, but the order of the elements within the parts does not matter (for example, when partitioning the vector [1,2,3,4], the partition {[1],[2,3,4]} is the same as {[1],[3,2,4]}, but distinct from {[2,3,4],[1]}).

Syntax

  • PT = vec_partitions(V,P)
  • PT = vec_partitions(V,P,SZ)

Argument descriptions

  • V: A vector to be partitioned.
  • P: A positive integer: the number of parts to partition P into.
  • SZ (optional, default [1,1,...,1]): A vector such that only the partitions with the property that the j-th part contains at least SZ(j) elements (for all j) are returned.

Examples

The following code generates all 14 partitions of the vector [1,2,3,4] into two parts. It then displays two of those partitions (the first and sixth).

>> pt = vec_partitions(1:4,2);
>> length(pt)

ans =

    14

>> celldisp(pt{1})
 
ans{1} =
 
     1

 
 
ans{2} =
 
     2     3     4

 
>> celldisp(pt{6})
 
ans{1} =
 
     1     3

 
 
ans{2} =
 
     2     4

Source code

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