This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Compositions of an integer

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


combinat::compositions(n, <MinPart = k>, <MaxPart = l>, <Length = m>)


combinat::compositions(n) returns all compositions of the nonnegative integer n.

A composition of a nonnegative integer n is a list of positive integers with total sum n.


Example 1

We output all compositions of the integer 4:


Example 2

It is possible to output only the compositions of a certain length:

combinat::compositions(4, Length=2)

Example 3

The options MinPart and MaxPart can be used to set constraints on the sizes of all parts. Using MaxPart, you can select compositions having only small entries. This is the list of the compositions of 4 with all parts at most 2:

combinat::compositions(4, MaxPart=2)

MinPart is complementary to MaxPart and selects compositions having only large parts (it takes a non-negative value). This is the list of the compositions of 4 with all parts at least 2:

combinat::compositions(4, MinPart=2)

By default, the parts of a composition have to be positive. This can be changed using the option MinPart. In the following example, the options Length and MinPart are combined together to obtain the list of the compositions of 4 with 3 nonnegative parts:

combinat::compositions(4, Length=3, MinPart=0)

If no length is given, MinPart=0 is not allowed.



Nonnegative integer



Option, specified as MinPart = k

Return only compositions consisting of integers greater or equal than k. The option MinPart = 0 is only allowed if also the option Length is given. Default is 1.


Option, specified as MaxPart = l

Return only compositions consisting of integers less or equal than l.


Option, specified as Length = m

Return only compositions consisting of exactly m integers.

Was this topic helpful?