File Exchange

image thumbnail

Set partition

version (6.99 KB) by Bruno Luong
List all partitions a set n elements


Updated 02 Jun 2009

View License

A set of of n elements can be partitioned in non empty subsets.

This package provide function to list all possible partitions. The number of partition is Bell's number.

The number of subsets that composed the partitions can be optionally specified. The number of partition is Stirling's number of the second kind.

Cite As

Bruno Luong (2020). Set partition (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

asim nadeem

The function is excellent. How can I store the partitions generated by this function to use them later in some other function. I want to store these partitions as they would be in billions for later use , in this way i will not have to calculate these again and again. Is there any online data that can store partitions for at least upto 20.i.e all the partitions of set {1,2,...,20} of length 4.


Thank you for your contribution to this NP problem in MATLAB! It's a quite essential but complicated problem in basic use while matlab doen't contain the relative function.

youngmo koo

Xuyang Tian

Thanks a lot for this. This is very helpful for my project!

Alex Saja

Thank you a lot! I needed these functions in my Optimization project

Thanks Bruno!!


Excellent codes, really fast!!


Is there an easy way to generate the partitions incrementally? I mean I'd like to use it for big N so it would be great if there was a way to generate a different partition a time, because of space limits.


Matt Fig

Very nice: smartly coded, fast, well implemented.

Bruno Luong

Darren, thank you for the suggestion. I'll add an example of usage. Functions with more efficient engine will be soon upgraded.

At last someone has implemented this functionality. Excellent!
I would only suggest that an example of usage and output could be helpful in the SetPartition file and that the first few lines of Bell and Stirling2nd could be rearranged to give meaningful H1 lines.


Partitions display function

Possibility to partition generic set elements (following Matt Fig's idea). New function to replace elements of a standard set partitioning list

Correct a BUG for N=0. Minor speed improvement.

Improve engine and example usage in the help as suggested Darren Rowland

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: PARTITIONS