How to go through each splits of a set?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
0 votes
Share a link to this question
Accepted Answer
1 vote
Hi @Merse Gaspar ,
You mentioned, “What is the most effective/shortest solution to this? I want to go through all possible splits of a set and I need the two subsets. For example, take the numbers 1 to 10, these have 2^10 bisections. These can be easily generated in this way: N=10; X = dec2bin(0:2^N-1); And then you could go through the rows of the X matrix.But my question is whether there is a shorter solution that would produce the two subsets quickly and directly.So these should be included in the output: “
To achieve the desired outcome of generating all possible splits of a set into two subsets, you can utilize a recursive approach or a combinatorial method. The challenge lies in efficiently producing the two subsets for each possible division of the set. Below, I will provide a detailed explanation of the approach, followed by the complete MATLAB code. My approach would be create a recursive function that will generate all combinations of the elements in the set. For each combination, you can derive the two subsets by determining which elements are included in the current combination and which are not or use Combinatorial Generation. Instead of generating binary representations, you can directly use MATLAB's built-in functions to generate combinations. This will allow us to avoid unnecessary iterations and directly obtain the subsets. Here is the complete MATLAB code that implements the above approach:
function generateSubsets(N) % Generate a vector of numbers from 1 to N numbers = 1:N;
% Initialize a cell array to hold the results
results = {}; % Loop through all possible sizes of the first subset
for k = 1:N-1
% Generate all combinations of size k
combinations = nchoosek(numbers, k); % Loop through each combination
for i = 1:size(combinations, 1)
% Current subset
subset1 = combinations(i, :);
% Remaining elements form the second subset
subset2 = setdiff(numbers, subset1); % Store the result
results{end+1} = {subset1, subset2}; %#ok<AGROW>
end
end % Display the results
for j = 1:length(results)
fprintf('[%s], [%s]\n', num2str(results{j}{1}), num2str(results{j}{2}));
end
end% Call the function with N = 10 generateSubsets(10);
For more information on nchoosek function, please refer to
https://www.mathworks.com/help/matlab/ref/nchoosek.html
Please see attached.

So, in the above script, function generateSubsets(N) takes an integer N as input, which defines the range of numbers from 1 to N. A vector numbers is created containing the integers from 1 to N. The nchoosek function is used to generate all combinations of the numbers for sizes ranging from 1 to N-1. This ensures that we do not create empty subsets. For each combination generated, the first subset is taken directly from the combination, while the second subset is derived using the setdiff function, which finds the elements not included in the first subset. The subsets are stored in a cell array results, which allows for dynamic storage of varying sizes of subsets. Finally, the results are printed in the specified format.
So, as you can realize now that by leveraging combinatorial functions, you avoid the overhead of binary representation and directly obtain the desired subsets. This approach is not only shorter but also more intuitive, making it easier to understand and maintain.
Hope this helps.
Please let me know if you have any further questions.
3 Comments
Share a link to this comment
Share a link to this comment
Hi @Voss,
In MATLAB, when generating subsets of a set, it is common to exclude the empty set from the results. This can be achieved by modifying the subset generation logic. The typical approach to generate all subsets of a set of size (N) is to use binary representation, where each bit represents the inclusion or exclusion of an element. However, if you want to exclude subsets that contain the empty set, you can adjust your code accordingly. I will provide a simple example of how to generate subsets while excluding those that contain the empty set, please execute the following code and let me know if this is what you meant in your provided comments.
function subsets = generateSubsets(set)
n = length(set);
totalSubsets = 2^n; % Total subsets including empty set
subsets = {}; % Initialize cell array for subsets
for i = 1:totalSubsets-1 % Exclude the empty set
subset = [];
for j = 1:n
if bitget(i-1, j) % Check if j-th element is included
subset = [subset, set(j)];
end
end
subsets{end+1} = subset; % Add the subset to the list
end
end% Example usage result = generateSubsets(1:10); disp(result);
In this script, it loops through all possible combinations of the set elements, excluding the empty set by starting the loop from 1 and going up to (2^N - 1). This results in (2^N - 1) subsets, which includes all non-empty subsets.
More Answers (1)
2 votes
Categories
Find more on Function Creation in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)