How to go through each splits of a set?

1 次查看(过去 30 天)
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:
[1], [2,3,4,5,6,7,8,9,10]
[2], [1,3,4,5,6,7,8,9,10]
...
[1,2], [3,4,5,6,7,8,9,10]
[1,3], [2,4,5,6,7,8,9,10]
...
[1,2,3], [4,5,6,7,8,9,10]

采纳的回答

Umar
Umar 2024-10-15

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 个评论
Umar
Umar 2024-10-15

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.

Voss
Voss 2024-10-15
编辑:Voss 2024-10-15
That includes the empty set and excludes the original set itself.
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);
Columns 1 through 15 {0x0 double} {[1]} {[2]} {[1 2]} {[3]} {[1 3]} {[2 3]} {[1 2 3]} {[4]} {[1 4]} {[2 4]} {[1 2 4]} {[3 4]} {[1 3 4]} {[2 3 4]} Columns 16 through 29 {[1 2 3 4]} {[5]} {[1 5]} {[2 5]} {[1 2 5]} {[3 5]} {[1 3 5]} {[2 3 5]} {[1 2 3 5]} {[4 5]} {[1 4 5]} {[2 4 5]} {[1 2 4 5]} {[3 4 5]} Columns 30 through 43 {[1 3 4 5]} {[2 3 4 5]} {[1 2 3 4 5]} {[6]} {[1 6]} {[2 6]} {[1 2 6]} {[3 6]} {[1 3 6]} {[2 3 6]} {[1 2 3 6]} {[4 6]} {[1 4 6]} {[2 4 6]} Columns 44 through 55 {[1 2 4 6]} {[3 4 6]} {[1 3 4 6]} {[2 3 4 6]} {[1 2 3 4 6]} {[5 6]} {[1 5 6]} {[2 5 6]} {[1 2 5 6]} {[3 5 6]} {[1 3 5 6]} {[2 3 5 6]} Columns 56 through 67 {[1 2 3 5 6]} {[4 5 6]} {[1 4 5 6]} {[2 4 5 6]} {[1 2 4 5 6]} {[3 4 5 6]} {[1 3 4 5 6]} {[2 3 4 5 6]} {[1 2 3 4 5 6]} {[7]} {[1 7]} {[2 7]} Columns 68 through 80 {[1 2 7]} {[3 7]} {[1 3 7]} {[2 3 7]} {[1 2 3 7]} {[4 7]} {[1 4 7]} {[2 4 7]} {[1 2 4 7]} {[3 4 7]} {[1 3 4 7]} {[2 3 4 7]} {[1 2 3 4 7]} Columns 81 through 92 {[5 7]} {[1 5 7]} {[2 5 7]} {[1 2 5 7]} {[3 5 7]} {[1 3 5 7]} {[2 3 5 7]} {[1 2 3 5 7]} {[4 5 7]} {[1 4 5 7]} {[2 4 5 7]} {[1 2 4 5 7]} Columns 93 through 104 {[3 4 5 7]} {[1 3 4 5 7]} {[2 3 4 5 7]} {[1 2 3 4 5 7]} {[6 7]} {[1 6 7]} {[2 6 7]} {[1 2 6 7]} {[3 6 7]} {[1 3 6 7]} {[2 3 6 7]} {[1 2 3 6 7]} Columns 105 through 115 {[4 6 7]} {[1 4 6 7]} {[2 4 6 7]} {[1 2 4 6 7]} {[3 4 6 7]} {[1 3 4 6 7]} {[2 3 4 6 7]} {[1 2 3 4 6 7]} {[5 6 7]} {[1 5 6 7]} {[2 5 6 7]} Columns 116 through 125 {[1 2 5 6 7]} {[3 5 6 7]} {[1 3 5 6 7]} {[2 3 5 6 7]} {[1 2 3 5 6 7]} {[4 5 6 7]} {[1 4 5 6 7]} {[2 4 5 6 7]} {[1 2 4 5 6 7]} {[3 4 5 6 7]} Columns 126 through 138 {[1 3 4 5 6 7]} {[2 3 4 5 6 7]} {[1 2 3 4 5 6 7]} {[8]} {[1 8]} {[2 8]} {[1 2 8]} {[3 8]} {[1 3 8]} {[2 3 8]} {[1 2 3 8]} {[4 8]} {[1 4 8]} Columns 139 through 150 {[2 4 8]} {[1 2 4 8]} {[3 4 8]} {[1 3 4 8]} {[2 3 4 8]} {[1 2 3 4 8]} {[5 8]} {[1 5 8]} {[2 5 8]} {[1 2 5 8]} {[3 5 8]} {[1 3 5 8]} Columns 151 through 161 {[2 3 5 8]} {[1 2 3 5 8]} {[4 5 8]} {[1 4 5 8]} {[2 4 5 8]} {[1 2 4 5 8]} {[3 4 5 8]} {[1 3 4 5 8]} {[2 3 4 5 8]} {[1 2 3 4 5 8]} {[6 8]} Columns 162 through 173 {[1 6 8]} {[2 6 8]} {[1 2 6 8]} {[3 6 8]} {[1 3 6 8]} {[2 3 6 8]} {[1 2 3 6 8]} {[4 6 8]} {[1 4 6 8]} {[2 4 6 8]} {[1 2 4 6 8]} {[3 4 6 8]} Columns 174 through 184 {[1 3 4 6 8]} {[2 3 4 6 8]} {[1 2 3 4 6 8]} {[5 6 8]} {[1 5 6 8]} {[2 5 6 8]} {[1 2 5 6 8]} {[3 5 6 8]} {[1 3 5 6 8]} {[2 3 5 6 8]} {[1 2 3 5 6 8]} Columns 185 through 195 {[4 5 6 8]} {[1 4 5 6 8]} {[2 4 5 6 8]} {[1 2 4 5 6 8]} {[3 4 5 6 8]} {[1 3 4 5 6 8]} {[2 3 4 5 6 8]} {[1 2 3 4 5 6 8]} {[7 8]} {[1 7 8]} {[2 7 8]} Columns 196 through 206 {[1 2 7 8]} {[3 7 8]} {[1 3 7 8]} {[2 3 7 8]} {[1 2 3 7 8]} {[4 7 8]} {[1 4 7 8]} {[2 4 7 8]} {[1 2 4 7 8]} {[3 4 7 8]} {[1 3 4 7 8]} Columns 207 through 217 {[2 3 4 7 8]} {[1 2 3 4 7 8]} {[5 7 8]} {[1 5 7 8]} {[2 5 7 8]} {[1 2 5 7 8]} {[3 5 7 8]} {[1 3 5 7 8]} {[2 3 5 7 8]} {[1 2 3 5 7 8]} {[4 5 7 8]} Columns 218 through 227 {[1 4 5 7 8]} {[2 4 5 7 8]} {[1 2 4 5 7 8]} {[3 4 5 7 8]} {[1 3 4 5 7 8]} {[2 3 4 5 7 8]} {[1 2 3 4 5 7 8]} {[6 7 8]} {[1 6 7 8]} {[2 6 7 8]} Columns 228 through 237 {[1 2 6 7 8]} {[3 6 7 8]} {[1 3 6 7 8]} {[2 3 6 7 8]} {[1 2 3 6 7 8]} {[4 6 7 8]} {[1 4 6 7 8]} {[2 4 6 7 8]} {[1 2 4 6 7 8]} {[3 4 6 7 8]} Columns 238 through 246 {[1 3 4 6 7 8]} {[2 3 4 6 7 8]} {[1 2 3 4 6 7 8]} {[5 6 7 8]} {[1 5 6 7 8]} {[2 5 6 7 8]} {[1 2 5 6 7 8]} {[3 5 6 7 8]} {[1 3 5 6 7 8]} Columns 247 through 255 {[2 3 5 6 7 8]} {[1 2 3 5 6 7 8]} {[4 5 6 7 8]} {[1 4 5 6 7 8]} {[2 4 5 6 7 8]} {[1 2 4 5 6 7 8]} {[3 4 5 6 7 8]} {[1 3 4 5 6 7 8]} {[2 3 4 5 6 7 8]} Columns 256 through 268 {[1 2 3 4 5 6 7 8]} {[9]} {[1 9]} {[2 9]} {[1 2 9]} {[3 9]} {[1 3 9]} {[2 3 9]} {[1 2 3 9]} {[4 9]} {[1 4 9]} {[2 4 9]} {[1 2 4 9]} Columns 269 through 280 {[3 4 9]} {[1 3 4 9]} {[2 3 4 9]} {[1 2 3 4 9]} {[5 9]} {[1 5 9]} {[2 5 9]} {[1 2 5 9]} {[3 5 9]} {[1 3 5 9]} {[2 3 5 9]} {[1 2 3 5 9]} Columns 281 through 292 {[4 5 9]} {[1 4 5 9]} {[2 4 5 9]} {[1 2 4 5 9]} {[3 4 5 9]} {[1 3 4 5 9]} {[2 3 4 5 9]} {[1 2 3 4 5 9]} {[6 9]} {[1 6 9]} {[2 6 9]} {[1 2 6 9]} Columns 293 through 303 {[3 6 9]} {[1 3 6 9]} {[2 3 6 9]} {[1 2 3 6 9]} {[4 6 9]} {[1 4 6 9]} {[2 4 6 9]} {[1 2 4 6 9]} {[3 4 6 9]} {[1 3 4 6 9]} {[2 3 4 6 9]} Columns 304 through 314 {[1 2 3 4 6 9]} {[5 6 9]} {[1 5 6 9]} {[2 5 6 9]} {[1 2 5 6 9]} {[3 5 6 9]} {[1 3 5 6 9]} {[2 3 5 6 9]} {[1 2 3 5 6 9]} {[4 5 6 9]} {[1 4 5 6 9]} Columns 315 through 325 {[2 4 5 6 9]} {[1 2 4 5 6 9]} {[3 4 5 6 9]} {[1 3 4 5 6 9]} {[2 3 4 5 6 9]} {[1 2 3 4 5 6 9]} {[7 9]} {[1 7 9]} {[2 7 9]} {[1 2 7 9]} {[3 7 9]} Columns 326 through 336 {[1 3 7 9]} {[2 3 7 9]} {[1 2 3 7 9]} {[4 7 9]} {[1 4 7 9]} {[2 4 7 9]} {[1 2 4 7 9]} {[3 4 7 9]} {[1 3 4 7 9]} {[2 3 4 7 9]} {[1 2 3 4 7 9]} Columns 337 through 347 {[5 7 9]} {[1 5 7 9]} {[2 5 7 9]} {[1 2 5 7 9]} {[3 5 7 9]} {[1 3 5 7 9]} {[2 3 5 7 9]} {[1 2 3 5 7 9]} {[4 5 7 9]} {[1 4 5 7 9]} {[2 4 5 7 9]} Columns 348 through 357 {[1 2 4 5 7 9]} {[3 4 5 7 9]} {[1 3 4 5 7 9]} {[2 3 4 5 7 9]} {[1 2 3 4 5 7 9]} {[6 7 9]} {[1 6 7 9]} {[2 6 7 9]} {[1 2 6 7 9]} {[3 6 7 9]} Columns 358 through 367 {[1 3 6 7 9]} {[2 3 6 7 9]} {[1 2 3 6 7 9]} {[4 6 7 9]} {[1 4 6 7 9]} {[2 4 6 7 9]} {[1 2 4 6 7 9]} {[3 4 6 7 9]} {[1 3 4 6 7 9]} {[2 3 4 6 7 9]} Columns 368 through 376 {[1 2 3 4 6 7 9]} {[5 6 7 9]} {[1 5 6 7 9]} {[2 5 6 7 9]} {[1 2 5 6 7 9]} {[3 5 6 7 9]} {[1 3 5 6 7 9]} {[2 3 5 6 7 9]} {[1 2 3 5 6 7 9]} Columns 377 through 385 {[4 5 6 7 9]} {[1 4 5 6 7 9]} {[2 4 5 6 7 9]} {[1 2 4 5 6 7 9]} {[3 4 5 6 7 9]} {[1 3 4 5 6 7 9]} {[2 3 4 5 6 7 9]} {[1 2 3 4 5 6 7 9]} {[8 9]} Columns 386 through 397 {[1 8 9]} {[2 8 9]} {[1 2 8 9]} {[3 8 9]} {[1 3 8 9]} {[2 3 8 9]} {[1 2 3 8 9]} {[4 8 9]} {[1 4 8 9]} {[2 4 8 9]} {[1 2 4 8 9]} {[3 4 8 9]} Columns 398 through 408 {[1 3 4 8 9]} {[2 3 4 8 9]} {[1 2 3 4 8 9]} {[5 8 9]} {[1 5 8 9]} {[2 5 8 9]} {[1 2 5 8 9]} {[3 5 8 9]} {[1 3 5 8 9]} {[2 3 5 8 9]} {[1 2 3 5 8 9]} Columns 409 through 418 {[4 5 8 9]} {[1 4 5 8 9]} {[2 4 5 8 9]} {[1 2 4 5 8 9]} {[3 4 5 8 9]} {[1 3 4 5 8 9]} {[2 3 4 5 8 9]} {[1 2 3 4 5 8 9]} {[6 8 9]} {[1 6 8 9]} Columns 419 through 428 {[2 6 8 9]} {[1 2 6 8 9]} {[3 6 8 9]} {[1 3 6 8 9]} {[2 3 6 8 9]} {[1 2 3 6 8 9]} {[4 6 8 9]} {[1 4 6 8 9]} {[2 4 6 8 9]} {[1 2 4 6 8 9]} Columns 429 through 438 {[3 4 6 8 9]} {[1 3 4 6 8 9]} {[2 3 4 6 8 9]} {[1 2 3 4 6 8 9]} {[5 6 8 9]} {[1 5 6 8 9]} {[2 5 6 8 9]} {[1 2 5 6 8 9]} {[3 5 6 8 9]} {[1 3 5 6 8 9]} Columns 439 through 447 {[2 3 5 6 8 9]} {[1 2 3 5 6 8 9]} {[4 5 6 8 9]} {[1 4 5 6 8 9]} {[2 4 5 6 8 9]} {[1 2 4 5 6 8 9]} {[3 4 5 6 8 9]} {[1 3 4 5 6 8 9]} {[2 3 4 5 6 8 9]} Columns 448 through 457 {[1 2 3 4 5 6 8 9]} {[7 8 9]} {[1 7 8 9]} {[2 7 8 9]} {[1 2 7 8 9]} {[3 7 8 9]} {[1 3 7 8 9]} {[2 3 7 8 9]} {[1 2 3 7 8 9]} {[4 7 8 9]} Columns 458 through 467 {[1 4 7 8 9]} {[2 4 7 8 9]} {[1 2 4 7 8 9]} {[3 4 7 8 9]} {[1 3 4 7 8 9]} {[2 3 4 7 8 9]} {[1 2 3 4 7 8 9]} {[5 7 8 9]} {[1 5 7 8 9]} {[2 5 7 8 9]} Columns 468 through 476 {[1 2 5 7 8 9]} {[3 5 7 8 9]} {[1 3 5 7 8 9]} {[2 3 5 7 8 9]} {[1 2 3 5 7 8 9]} {[4 5 7 8 9]} {[1 4 5 7 8 9]} {[2 4 5 7 8 9]} {[1 2 4 5 7 8 9]} Columns 477 through 485 {[3 4 5 7 8 9]} {[1 3 4 5 7 8 9]} {[2 3 4 5 7 8 9]} {[1 2 3 4 5 7 8 9]} {[6 7 8 9]} {[1 6 7 8 9]} {[2 6 7 8 9]} {[1 2 6 7 8 9]} {[3 6 7 8 9]} Columns 486 through 494 {[1 3 6 7 8 9]} {[2 3 6 7 8 9]} {[1 2 3 6 7 8 9]} {[4 6 7 8 9]} {[1 4 6 7 8 9]} {[2 4 6 7 8 9]} {[1 2 4 6 7 8 9]} {[3 4 6 7 8 9]} {[1 3 4 6 7 8 9]} Columns 495 through 503 {[2 3 4 6 7 8 9]} {[1 2 3 4 6 7 8 9]} {[5 6 7 8 9]} {[1 5 6 7 8 9]} {[2 5 6 7 8 9]} {[1 2 5 6 7 8 9]} {[3 5 6 7 8 9]} {[1 3 5 6 7 8 9]} {[2 3 5 6 7 8 9]} Columns 504 through 511 {[1 2 3 5 6 7 8 9]} {[4 5 6 7 8 9]} {[1 4 5 6 7 8 9]} {[2 4 5 6 7 8 9]} {[1 2 4 5 6 7 8 9]} {[3 4 5 6 7 8 9]} {[1 3 4 5 6 7 8 9]} {[2 3 4 5 6 7 8 9]} Columns 512 through 523 {[1 2 3 4 5 6 7 8 9]} {[10]} {[1 10]} {[2 10]} {[1 2 10]} {[3 10]} {[1 3 10]} {[2 3 10]} {[1 2 3 10]} {[4 10]} {[1 4 10]} {[2 4 10]} Columns 524 through 534 {[1 2 4 10]} {[3 4 10]} {[1 3 4 10]} {[2 3 4 10]} {[1 2 3 4 10]} {[5 10]} {[1 5 10]} {[2 5 10]} {[1 2 5 10]} {[3 5 10]} {[1 3 5 10]} Columns 535 through 544 {[2 3 5 10]} {[1 2 3 5 10]} {[4 5 10]} {[1 4 5 10]} {[2 4 5 10]} {[1 2 4 5 10]} {[3 4 5 10]} {[1 3 4 5 10]} {[2 3 4 5 10]} {[1 2 3 4 5 10]} Columns 545 through 555 {[6 10]} {[1 6 10]} {[2 6 10]} {[1 2 6 10]} {[3 6 10]} {[1 3 6 10]} {[2 3 6 10]} {[1 2 3 6 10]} {[4 6 10]} {[1 4 6 10]} {[2 4 6 10]} Columns 556 through 565 {[1 2 4 6 10]} {[3 4 6 10]} {[1 3 4 6 10]} {[2 3 4 6 10]} {[1 2 3 4 6 10]} {[5 6 10]} {[1 5 6 10]} {[2 5 6 10]} {[1 2 5 6 10]} {[3 5 6 10]} Columns 566 through 574 {[1 3 5 6 10]} {[2 3 5 6 10]} {[1 2 3 5 6 10]} {[4 5 6 10]} {[1 4 5 6 10]} {[2 4 5 6 10]} {[1 2 4 5 6 10]} {[3 4 5 6 10]} {[1 3 4 5 6 10]} Columns 575 through 585 {[2 3 4 5 6 10]} {[1 2 3 4 5 6 10]} {[7 10]} {[1 7 10]} {[2 7 10]} {[1 2 7 10]} {[3 7 10]} {[1 3 7 10]} {[2 3 7 10]} {[1 2 3 7 10]} {[4 7 10]} Columns 586 through 595 {[1 4 7 10]} {[2 4 7 10]} {[1 2 4 7 10]} {[3 4 7 10]} {[1 3 4 7 10]} {[2 3 4 7 10]} {[1 2 3 4 7 10]} {[5 7 10]} {[1 5 7 10]} {[2 5 7 10]} Columns 596 through 605 {[1 2 5 7 10]} {[3 5 7 10]} {[1 3 5 7 10]} {[2 3 5 7 10]} {[1 2 3 5 7 10]} {[4 5 7 10]} {[1 4 5 7 10]} {[2 4 5 7 10]} {[1 2 4 5 7 10]} {[3 4 5 7 10]} Columns 606 through 615 {[1 3 4 5 7 10]} {[2 3 4 5 7 10]} {[1 2 3 4 5 7 10]} {[6 7 10]} {[1 6 7 10]} {[2 6 7 10]} {[1 2 6 7 10]} {[3 6 7 10]} {[1 3 6 7 10]} {[2 3 6 7 10]} Columns 616 through 624 {[1 2 3 6 7 10]} {[4 6 7 10]} {[1 4 6 7 10]} {[2 4 6 7 10]} {[1 2 4 6 7 10]} {[3 4 6 7 10]} {[1 3 4 6 7 10]} {[2 3 4 6 7 10]} {[1 2 3 4 6 7 10]} Columns 625 through 633 {[5 6 7 10]} {[1 5 6 7 10]} {[2 5 6 7 10]} {[1 2 5 6 7 10]} {[3 5 6 7 10]} {[1 3 5 6 7 10]} {[2 3 5 6 7 10]} {[1 2 3 5 6 7 10]} {[4 5 6 7 10]} Columns 634 through 642 {[1 4 5 6 7 10]} {[2 4 5 6 7 10]} {[1 2 4 5 6 7 10]} {[3 4 5 6 7 10]} {[1 3 4 5 6 7 10]} {[2 3 4 5 6 7 10]} {[1 2 3 4 5 6 7 10]} {[8 10]} {[1 8 10]} Columns 643 through 653 {[2 8 10]} {[1 2 8 10]} {[3 8 10]} {[1 3 8 10]} {[2 3 8 10]} {[1 2 3 8 10]} {[4 8 10]} {[1 4 8 10]} {[2 4 8 10]} {[1 2 4 8 10]} {[3 4 8 10]} Columns 654 through 663 {[1 3 4 8 10]} {[2 3 4 8 10]} {[1 2 3 4 8 10]} {[5 8 10]} {[1 5 8 10]} {[2 5 8 10]} {[1 2 5 8 10]} {[3 5 8 10]} {[1 3 5 8 10]} {[2 3 5 8 10]} Columns 664 through 672 {[1 2 3 5 8 10]} {[4 5 8 10]} {[1 4 5 8 10]} {[2 4 5 8 10]} {[1 2 4 5 8 10]} {[3 4 5 8 10]} {[1 3 4 5 8 10]} {[2 3 4 5 8 10]} {[1 2 3 4 5 8 10]} Columns 673 through 682 {[6 8 10]} {[1 6 8 10]} {[2 6 8 10]} {[1 2 6 8 10]} {[3 6 8 10]} {[1 3 6 8 10]} {[2 3 6 8 10]} {[1 2 3 6 8 10]} {[4 6 8 10]} {[1 4 6 8 10]} Columns 683 through 691 {[2 4 6 8 10]} {[1 2 4 6 8 10]} {[3 4 6 8 10]} {[1 3 4 6 8 10]} {[2 3 4 6 8 10]} {[1 2 3 4 6 8 10]} {[5 6 8 10]} {[1 5 6 8 10]} {[2 5 6 8 10]} Columns 692 through 700 {[1 2 5 6 8 10]} {[3 5 6 8 10]} {[1 3 5 6 8 10]} {[2 3 5 6 8 10]} {[1 2 3 5 6 8 10]} {[4 5 6 8 10]} {[1 4 5 6 8 10]} {[2 4 5 6 8 10]} {[1 2 4 5 6 8 10]} Columns 701 through 709 {[3 4 5 6 8 10]} {[1 3 4 5 6 8 10]} {[2 3 4 5 6 8 10]} {[1 2 3 4 5 6 8 10]} {[7 8 10]} {[1 7 8 10]} {[2 7 8 10]} {[1 2 7 8 10]} {[3 7 8 10]} Columns 710 through 718 {[1 3 7 8 10]} {[2 3 7 8 10]} {[1 2 3 7 8 10]} {[4 7 8 10]} {[1 4 7 8 10]} {[2 4 7 8 10]} {[1 2 4 7 8 10]} {[3 4 7 8 10]} {[1 3 4 7 8 10]} Columns 719 through 727 {[2 3 4 7 8 10]} {[1 2 3 4 7 8 10]} {[5 7 8 10]} {[1 5 7 8 10]} {[2 5 7 8 10]} {[1 2 5 7 8 10]} {[3 5 7 8 10]} {[1 3 5 7 8 10]} {[2 3 5 7 8 10]} Columns 728 through 735 {[1 2 3 5 7 8 10]} {[4 5 7 8 10]} {[1 4 5 7 8 10]} {[2 4 5 7 8 10]} {[1 2 4 5 7 8 10]} {[3 4 5 7 8 10]} {[1 3 4 5 7 8 10]} {[2 3 4 5 7 8 10]} Columns 736 through 744 {[1 2 3 4 5 7 8 10]} {[6 7 8 10]} {[1 6 7 8 10]} {[2 6 7 8 10]} {[1 2 6 7 8 10]} {[3 6 7 8 10]} {[1 3 6 7 8 10]} {[2 3 6 7 8 10]} {[1 2 3 6 7 8 10]} Columns 745 through 752 {[4 6 7 8 10]} {[1 4 6 7 8 10]} {[2 4 6 7 8 10]} {[1 2 4 6 7 8 10]} {[3 4 6 7 8 10]} {[1 3 4 6 7 8 10]} {[2 3 4 6 7 8 10]} {[1 2 3 4 6 7 8 10]} Columns 753 through 760 {[5 6 7 8 10]} {[1 5 6 7 8 10]} {[2 5 6 7 8 10]} {[1 2 5 6 7 8 10]} {[3 5 6 7 8 10]} {[1 3 5 6 7 8 10]} {[2 3 5 6 7 8 10]} {[1 2 3 5 6 7 8 10]} Columns 761 through 768 {[4 5 6 7 8 10]} {[1 4 5 6 7 8 10]} {[2 4 5 6 7 8 10]} {[1 2 4 5 6 7 8 10]} {[3 4 5 6 7 8 10]} {[1 3 4 5 6 7 8 10]} {[2 3 4 5 6 7 8 10]} {1x9 double} Columns 769 through 779 {[9 10]} {[1 9 10]} {[2 9 10]} {[1 2 9 10]} {[3 9 10]} {[1 3 9 10]} {[2 3 9 10]} {[1 2 3 9 10]} {[4 9 10]} {[1 4 9 10]} {[2 4 9 10]} Columns 780 through 789 {[1 2 4 9 10]} {[3 4 9 10]} {[1 3 4 9 10]} {[2 3 4 9 10]} {[1 2 3 4 9 10]} {[5 9 10]} {[1 5 9 10]} {[2 5 9 10]} {[1 2 5 9 10]} {[3 5 9 10]} Columns 790 through 798 {[1 3 5 9 10]} {[2 3 5 9 10]} {[1 2 3 5 9 10]} {[4 5 9 10]} {[1 4 5 9 10]} {[2 4 5 9 10]} {[1 2 4 5 9 10]} {[3 4 5 9 10]} {[1 3 4 5 9 10]} Columns 799 through 808 {[2 3 4 5 9 10]} {[1 2 3 4 5 9 10]} {[6 9 10]} {[1 6 9 10]} {[2 6 9 10]} {[1 2 6 9 10]} {[3 6 9 10]} {[1 3 6 9 10]} {[2 3 6 9 10]} {[1 2 3 6 9 10]} Columns 809 through 817 {[4 6 9 10]} {[1 4 6 9 10]} {[2 4 6 9 10]} {[1 2 4 6 9 10]} {[3 4 6 9 10]} {[1 3 4 6 9 10]} {[2 3 4 6 9 10]} {[1 2 3 4 6 9 10]} {[5 6 9 10]} Columns 818 through 826 {[1 5 6 9 10]} {[2 5 6 9 10]} {[1 2 5 6 9 10]} {[3 5 6 9 10]} {[1 3 5 6 9 10]} {[2 3 5 6 9 10]} {[1 2 3 5 6 9 10]} {[4 5 6 9 10]} {[1 4 5 6 9 10]} Columns 827 through 835 {[2 4 5 6 9 10]} {[1 2 4 5 6 9 10]} {[3 4 5 6 9 10]} {[1 3 4 5 6 9 10]} {[2 3 4 5 6 9 10]} {[1 2 3 4 5 6 9 10]} {[7 9 10]} {[1 7 9 10]} {[2 7 9 10]} Columns 836 through 845 {[1 2 7 9 10]} {[3 7 9 10]} {[1 3 7 9 10]} {[2 3 7 9 10]} {[1 2 3 7 9 10]} {[4 7 9 10]} {[1 4 7 9 10]} {[2 4 7 9 10]} {[1 2 4 7 9 10]} {[3 4 7 9 10]} Columns 846 through 854 {[1 3 4 7 9 10]} {[2 3 4 7 9 10]} {[1 2 3 4 7 9 10]} {[5 7 9 10]} {[1 5 7 9 10]} {[2 5 7 9 10]} {[1 2 5 7 9 10]} {[3 5 7 9 10]} {[1 3 5 7 9 10]} Columns 855 through 862 {[2 3 5 7 9 10]} {[1 2 3 5 7 9 10]} {[4 5 7 9 10]} {[1 4 5 7 9 10]} {[2 4 5 7 9 10]} {[1 2 4 5 7 9 10]} {[3 4 5 7 9 10]} {[1 3 4 5 7 9 10]} Columns 863 through 871 {[2 3 4 5 7 9 10]} {[1 2 3 4 5 7 9 10]} {[6 7 9 10]} {[1 6 7 9 10]} {[2 6 7 9 10]} {[1 2 6 7 9 10]} {[3 6 7 9 10]} {[1 3 6 7 9 10]} {[2 3 6 7 9 10]} Columns 872 through 879 {[1 2 3 6 7 9 10]} {[4 6 7 9 10]} {[1 4 6 7 9 10]} {[2 4 6 7 9 10]} {[1 2 4 6 7 9 10]} {[3 4 6 7 9 10]} {[1 3 4 6 7 9 10]} {[2 3 4 6 7 9 10]} Columns 880 through 887 {[1 2 3 4 6 7 9 10]} {[5 6 7 9 10]} {[1 5 6 7 9 10]} {[2 5 6 7 9 10]} {[1 2 5 6 7 9 10]} {[3 5 6 7 9 10]} {[1 3 5 6 7 9 10]} {[2 3 5 6 7 9 10]} Columns 888 through 894 {[1 2 3 5 6 7 9 10]} {[4 5 6 7 9 10]} {[1 4 5 6 7 9 10]} {[2 4 5 6 7 9 10]} {[1 2 4 5 6 7 9 10]} {[3 4 5 6 7 9 10]} {[1 3 4 5 6 7 9 10]} Columns 895 through 904 {[2 3 4 5 6 7 9 10]} {1x9 double} {[8 9 10]} {[1 8 9 10]} {[2 8 9 10]} {[1 2 8 9 10]} {[3 8 9 10]} {[1 3 8 9 10]} {[2 3 8 9 10]} {[1 2 3 8 9 10]} Columns 905 through 913 {[4 8 9 10]} {[1 4 8 9 10]} {[2 4 8 9 10]} {[1 2 4 8 9 10]} {[3 4 8 9 10]} {[1 3 4 8 9 10]} {[2 3 4 8 9 10]} {[1 2 3 4 8 9 10]} {[5 8 9 10]} Columns 914 through 922 {[1 5 8 9 10]} {[2 5 8 9 10]} {[1 2 5 8 9 10]} {[3 5 8 9 10]} {[1 3 5 8 9 10]} {[2 3 5 8 9 10]} {[1 2 3 5 8 9 10]} {[4 5 8 9 10]} {[1 4 5 8 9 10]} Columns 923 through 930 {[2 4 5 8 9 10]} {[1 2 4 5 8 9 10]} {[3 4 5 8 9 10]} {[1 3 4 5 8 9 10]} {[2 3 4 5 8 9 10]} {[1 2 3 4 5 8 9 10]} {[6 8 9 10]} {[1 6 8 9 10]} Columns 931 through 939 {[2 6 8 9 10]} {[1 2 6 8 9 10]} {[3 6 8 9 10]} {[1 3 6 8 9 10]} {[2 3 6 8 9 10]} {[1 2 3 6 8 9 10]} {[4 6 8 9 10]} {[1 4 6 8 9 10]} {[2 4 6 8 9 10]} Columns 940 through 947 {[1 2 4 6 8 9 10]} {[3 4 6 8 9 10]} {[1 3 4 6 8 9 10]} {[2 3 4 6 8 9 10]} {[1 2 3 4 6 8 9 10]} {[5 6 8 9 10]} {[1 5 6 8 9 10]} {[2 5 6 8 9 10]} Columns 948 through 955 {[1 2 5 6 8 9 10]} {[3 5 6 8 9 10]} {[1 3 5 6 8 9 10]} {[2 3 5 6 8 9 10]} {[1 2 3 5 6 8 9 10]} {[4 5 6 8 9 10]} {[1 4 5 6 8 9 10]} {[2 4 5 6 8 9 10]} Columns 956 through 963 {[1 2 4 5 6 8 9 10]} {[3 4 5 6 8 9 10]} {[1 3 4 5 6 8 9 10]} {[2 3 4 5 6 8 9 10]} {1x9 double} {[7 8 9 10]} {[1 7 8 9 10]} {[2 7 8 9 10]} Columns 964 through 972 {[1 2 7 8 9 10]} {[3 7 8 9 10]} {[1 3 7 8 9 10]} {[2 3 7 8 9 10]} {[1 2 3 7 8 9 10]} {[4 7 8 9 10]} {[1 4 7 8 9 10]} {[2 4 7 8 9 10]} {[1 2 4 7 8 9 10]} Columns 973 through 980 {[3 4 7 8 9 10]} {[1 3 4 7 8 9 10]} {[2 3 4 7 8 9 10]} {[1 2 3 4 7 8 9 10]} {[5 7 8 9 10]} {[1 5 7 8 9 10]} {[2 5 7 8 9 10]} {[1 2 5 7 8 9 10]} Columns 981 through 988 {[3 5 7 8 9 10]} {[1 3 5 7 8 9 10]} {[2 3 5 7 8 9 10]} {[1 2 3 5 7 8 9 10]} {[4 5 7 8 9 10]} {[1 4 5 7 8 9 10]} {[2 4 5 7 8 9 10]} {[1 2 4 5 7 8 9 10]} Columns 989 through 996 {[3 4 5 7 8 9 10]} {[1 3 4 5 7 8 9 10]} {[2 3 4 5 7 8 9 10]} {1x9 double} {[6 7 8 9 10]} {[1 6 7 8 9 10]} {[2 6 7 8 9 10]} {[1 2 6 7 8 9 10]} Columns 997 through 1004 {[3 6 7 8 9 10]} {[1 3 6 7 8 9 10]} {[2 3 6 7 8 9 10]} {[1 2 3 6 7 8 9 10]} {[4 6 7 8 9 10]} {[1 4 6 7 8 9 10]} {[2 4 6 7 8 9 10]} {[1 2 4 6 7 8 9 10]} Columns 1005 through 1012 {[3 4 6 7 8 9 10]} {[1 3 4 6 7 8 9 10]} {[2 3 4 6 7 8 9 10]} {1x9 double} {[5 6 7 8 9 10]} {[1 5 6 7 8 9 10]} {[2 5 6 7 8 9 10]} {[1 2 5 6 7 8 9 10]} Columns 1013 through 1020 {[3 5 6 7 8 9 10]} {[1 3 5 6 7 8 9 10]} {[2 3 5 6 7 8 9 10]} {1x9 double} {[4 5 6 7 8 9 10]} {[1 4 5 6 7 8 9 10]} {[2 4 5 6 7 8 9 10]} {1x9 double} Columns 1021 through 1023 {[3 4 5 6 7 8 9 10]} {1x9 double} {1x9 double}
siz = cellfun(@numel,result);
nnz(siz == 0) % one empty set found
ans = 1
nnz(siz == 10) % no length-10 sets found
ans = 0

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2024-10-15
dec2bin(0:2^N-1) is pretty short and is fairly efficient.
N=10;
X = dec2bin(0:2^N-1);
partA = arrayfun(@(row) find(X(row,:)=='1'), (1:size(X,1)).', 'uniform', 0);
partB = arrayfun(@(row) find(X(row,:)=='0'), (1:size(X,1)).', 'uniform', 0);
partitions = [partA, partB]
partitions = 1024x2 cell array
{1x0 double} {[1 2 3 4 5 6 7 8 9 10]} {[ 10]} {[ 1 2 3 4 5 6 7 8 9]} {[ 9]} {[ 1 2 3 4 5 6 7 8 10]} {[ 9 10]} {[ 1 2 3 4 5 6 7 8]} {[ 8]} {[ 1 2 3 4 5 6 7 9 10]} {[ 8 10]} {[ 1 2 3 4 5 6 7 9]} {[ 8 9]} {[ 1 2 3 4 5 6 7 10]} {[ 8 9 10]} {[ 1 2 3 4 5 6 7]} {[ 7]} {[ 1 2 3 4 5 6 8 9 10]} {[ 7 10]} {[ 1 2 3 4 5 6 8 9]} {[ 7 9]} {[ 1 2 3 4 5 6 8 10]} {[ 7 9 10]} {[ 1 2 3 4 5 6 8]} {[ 7 8]} {[ 1 2 3 4 5 6 9 10]} {[ 7 8 10]} {[ 1 2 3 4 5 6 9]} {[ 7 8 9]} {[ 1 2 3 4 5 6 10]} {[7 8 9 10]} {[ 1 2 3 4 5 6]}

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by