Advertisement
eacousineau

Option Overlay - Permutations

Sep 20th, 2011
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.38 KB | None | 0 0
  1. % optPermute()
  2. % Generate a set of full factorial permutations based on arguments given as
  3. % fields in a structure.
  4. % Meant to be used stand-alone or in conjunction with optOverlay()
  5. %
  6. %   optVars = struct('mass', {{5, 10, 15}}, 'pos', {{1, 2}}, 'extra', struct('x', {{2, 3}}))
  7. %   optSet = optPermute(optVars);
  8.  
  9. function [optSet] = optPermute(optVars)
  10.     optSet = {};
  11.    
  12.     %Test overlay?
  13.    
  14.     % Full factorial experiment
  15.     fields = fieldnames(optVars);
  16.     fieldCount = length(fields);
  17.    
  18.     % Start recursion
  19.     optBlank = struct();
  20.     doRecursion(optBlank, 1);
  21.    
  22.     function doRecursion(optCur, index)
  23.         if index <= fieldCount
  24.             field = fields{index};
  25.             value = optVars.(field);
  26.             if iscell(value)
  27.                 % Loop through, concatenate
  28.                 for i = 1:length(value)
  29.                     optCur.(field) = value{i};
  30.                     % Recurse through the rest of the options
  31.                     doRecursion(optCur, index + 1);
  32.                 end
  33.             elseif isstruct(value)
  34.                 % Recurse with a new set of options for that given option...
  35.                 % Get the set of options for that set
  36.                 % Incorporate these into permutations
  37.                 optSubSet = optPermute(value);
  38.                 % And like normal
  39.                 for i = 1:length(optSubSet)
  40.                     optCur.(field) = optSubSet{i};
  41.                     % And so on
  42.                     doRecursion(optCur, index + 1);
  43.                 end
  44.             end
  45.         else
  46.             % End of permute
  47.             % Add final options to the list of permutations
  48.             optSet{end + 1} = optCur;
  49.         end
  50.     end
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement