Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all permutations (with repetition) in MATLAB?

Suppose I have 4 letters and I want to arrange them in 3 places (repetition allowed), so I would have 43=64 possible permutations. How can I compute and print them?

like image 552
mina mohamadi Avatar asked Sep 03 '13 11:09

mina mohamadi


Video Answer


2 Answers

Simplifying Amro's answer, you could use this:

%// Sample data
x = 'ABCD';                 %// Set of possible letters
K = 3;                      %// Length of each permutation

%// Create all possible permutations (with repetition) of letters stored in x
C = cell(K, 1);             %// Preallocate a cell array
[C{:}] = ndgrid(x);         %// Create K grids of values
y = cellfun(@(x){x(:)}, C); %// Convert grids to column vectors
y = [y{:}];                 %// Obtain all permutations

Matrix y should store the permutations you're after.

like image 194
Eitan T Avatar answered Oct 04 '22 08:10

Eitan T


How about the function N_PERMUTE_K from the File Exchange?

like image 45
Milan Avatar answered Oct 04 '22 07:10

Milan