Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.66 KB | None | 0 0
  1. script.m
  2.  
  3.     % Define funciton to prepend a cell x with a variable i
  4.     cellprepend = @(x,i) {[i x]};
  5.    
  6.     % Execute and time function
  7.     tic;
  8.     a = allcomb(cellprepend,8);   % Solution in a
  9.     toc;
  10.  
  11. allcomb.m
  12.  
  13.     function a = allcomb( cellprepend, n )
  14.         % Add entire block as a combination
  15.         a{1} = n;
  16.        
  17.         % Exit recursion if block size 1
  18.         if n == 1
  19.             return;
  20.         end
  21.        
  22.         % Recurse cutting blocks at different segments
  23.         for i = 1:n-1
  24.             b = allcomb(cellprepend,n-i);
  25.             a = [a cellfun( cellprepend, b, num2cell( stretchmat( i, b ) ) )];
  26.         end
  27.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement