matlab - Unique combinations of a beaded necklace -
matlab - Unique combinations of a beaded necklace -
this question has reply here:
generate possible combinations of elements of vectors (cartesian product) 3 answersso i'm writing programme determine unique combinations of beaded necklace, can't seem right. rules can't have same necklace forwards , backwards, , can't have same necklace 1 bead beingness slid around other end. i've attached pictures clarify.
i wrote code it, , thought had achieved trying do, it's not working correctly.
n = [1 2 3 4 2 4]; % greenish = 1 % bluish = 2 % yellowish = 3 % reddish = 4 p = perms(n); total = max(size(p)); = 1:max(size(p)) q = p; q(i) = []; j = 1:max(size(q)) if isequal(p(i),fliplr(q(j))) total = total - 1; elseif isequal(p(i),circshift(q(j),[1,1])) total = total - 1; elseif isequal(p(i),circshift(q(j),[length(q(j))-1,length(q(j))-1])) total = total - 1; end disp(total) end end
logically, makes sense me, crazy.
you should p = unique(p,'rows')
before loops. see why, phone call perms([1 1 1])
@ command line.
there few issues here:
1) p
, perms, 2d matrix, each perm need p(i,:)
row. p(i)
single number.
2) don't remove wrong answers list, check against them twice. example, first in list [1 2 3 4 2 4];
, sec [4 2 4 3 2 1];
. fliplr
check compare these 2 combinations twice, 1 time in first loop around, 1 time in second.
3) if want create sure permutation rotation excluded (not moving 1 bead around), you'll need more circshift.
consider using ismember
rows
alternative 1 time again compare single row (e.g. flipped version of row you're checking) entire matrix.
matlab for-loop unique permutation
Comments
Post a Comment