i want to sample all the possibilities ranging from 5 to 25 layers. however, there are some contraints. for example, there should be no more than 3 45 deg. layers in a row. in matlab

You can use a loop to generate possible layer combinations ranging from 5 to 25 layers while satisfying the constraints. Here's an example code:

main.m
min_layers = 5;
max_layers = 25;
max_45deg_streak = 3;

for n_layers = min_layers:max_layers
    % Generate all possible combinations of 0 and 1 representing layer types
    layer_combinations = dec2bin(0:2^n_layers-1) - '0';
    
    % Filter combinations that have more than 3 45 degree layers in a row
    valid_combinations = all(conv2(layer_combinations, ones(1, max_45deg_streak), 'valid') < max_45deg_streak, 2);
    
    % Print the valid combinations for this number of layers
    fprintf("Valid combinations for %d layers:\n", n_layers);
    disp(layer_combinations(valid_combinations, :));
end
605 chars
16 lines

This code first generates all possible layer combinations for each number of layers in the given range using dec2bin and binary subtraction. It then applies the constraint by using conv2 to count the number of consecutive 1s (i.e., 45 degree layers) within a rolling window of length max_45deg_streak. If any such streak is longer than the maximum allowed, the combination is flagged as invalid.

The code prints the valid combinations for each number of layers, but you can modify it for your specific use case.

related categories

gistlibby LogSnag