Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Efficient MxN

By: a guest on Oct 15th, 2013  |  syntax: None  |  size: 0.52 KB  |  views: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. % Much more efficient MxN:
  2. % Note, we rotate the matrix here so rows and columns are also swapped,
  3. % this is because the reshape function reads column-wise.
  4. % Uses combn function:
  5. % http://www.mathworks.com.au/matlabcentral/fileexchange/7147-combn-4-3
  6.  
  7. n = 2; % number of rows
  8. m = 3; % number of columns
  9. num_list = [0 1 2]; % List of numbers to choose from.
  10.  
  11. num_com = size(num_list,2) .^ (m*n); %Total number of combinations
  12.  
  13. output = mat2cell(reshape(combn(num_list, m*n)', n, m*num_com), n, repmat(m,1,num_com));