matlab - How to calculate cyclic permutation of an array? -
how calculate cyclic permutation of array easy [ 1 2 3 4 ] in matlab . there function can directly compute circular permutation in matlab .
i'm assuming cyclic permutation mean this (1 2 3 4
considered same permutation cyclic shift 2 3 4 1
; want permutations different cyclic shift of each other):
one possible approach generate permutations, identify circularly equal, , keep 1 each group. done keeping permutations begin given index, example 1. follows problem can solved generating permutations of elements 2, 3, ... , attaching element 1 in front:
x = [ 1 2 3 4 ]; %// data p = perms(x(2:end)); %// generate permutations of x(2), x(3), ... p = [ repmat(x(1),size(p,1),1) p ]; %// attach x(1) in front of each permutation
in example, result is:
p = 1 2 3 4 1 2 4 3 1 3 2 4 1 3 4 2 1 4 2 3 1 4 3 2
or, if mean this (you want generate permutations cyclic shifts of each other: 1 2 3 4
, 2 3 4 1
, etc; 1 3 2 4
not allowed):
x = [ 1 2 3 4 ]; %// data n = numel(x); ii = mod(bsxfun(@plus, 1:n, (0:n-1).')-1, n) + 1; p = x(ii);
result:
p = 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3
Comments
Post a Comment