Я думаю, что это проще, если вы думаете о состоянии мрамора, а не о состоянии цвета.Затем при необходимости преобразуйте из мраморных состояний в цветовые.Например, если вы думаете о упорядоченном списке всех возможных состояний мрамора, вы можете создать функцию, которая выглядит следующим образом (используя все нули в качестве флага «все готово»):
function state = nextmarblestate(state, nMarbles, nColors)
%Increment the marble state by 1
%Add one to the last marble
state(end) = state(end)+1;
%Propogate change towards the first marble as colors overflow
for ixCurrent = nMarbles:-1:2
if state(ixCurrent) > nColors;
state(ixCurrent) = 1;
state(ixCurrent-1) = state(ixCurrent-1)+1;
else
return;
end
end
%Check to see if we are done (reset to 0)
if state(1) > nColors
state = zeros(1,nMarbles);
end
Затем вы можете написатьбыстрый цикл, чтобы пройти через все мраморные состояния, например:
nMarbles = 2;
nColors = 8;
marblestate = ones(1,nMarbles);
while sum(marblestate)>0
disp(['Marblestate = [' num2str(marblestate) ']'])
marblestate = nextmarblestate(marblestate, nMarbles, nColors);
end
Или, чтобы получить желаемый результат, напишите преобразование из мраморного состояния в цветное состояние, например так (извините за краткость здесь,это может быть расширено до более красивой версии):
marbleState2colorState = @(marblestate) arrayfun(@(color)sum(marblestate==color), 1:nColors);
И затем, используя эту функцию преобразования, разверните вышеуказанный цикл while следующим образом:
marblestate = ones(1,nMarbles);
while sum(marblestate)>0
disp(['Marblestate = [' num2str(marblestate) '], Colorstate = [' num2str(marbleState2colorState(marblestate)) ']'])
marblestate = nextmarblestate(marblestate, nMarbles, nColors);
end
, который выдает следующий вывод:
Marblestate = [1 1], Colorstate = [2 0 0 0 0 0 0 0]
Marblestate = [1 2], Colorstate = [1 1 0 0 0 0 0 0]
Marblestate = [1 3], Colorstate = [1 0 1 0 0 0 0 0]
Marblestate = [1 4], Colorstate = [1 0 0 1 0 0 0 0]
Marblestate = [1 5], Colorstate = [1 0 0 0 1 0 0 0]
Marblestate = [1 6], Colorstate = [1 0 0 0 0 1 0 0]
Marblestate = [1 7], Colorstate = [1 0 0 0 0 0 1 0]
Marblestate = [1 8], Colorstate = [1 0 0 0 0 0 0 1]
Marblestate = [2 1], Colorstate = [1 1 0 0 0 0 0 0]
Marblestate = [2 2], Colorstate = [0 2 0 0 0 0 0 0]
Marblestate = [2 3], Colorstate = [0 1 1 0 0 0 0 0]
Marblestate = [2 4], Colorstate = [0 1 0 1 0 0 0 0]
Marblestate = [2 5], Colorstate = [0 1 0 0 1 0 0 0]
Marblestate = [2 6], Colorstate = [0 1 0 0 0 1 0 0]
Marblestate = [2 7], Colorstate = [0 1 0 0 0 0 1 0]
Marblestate = [2 8], Colorstate = [0 1 0 0 0 0 0 1]
Marblestate = [3 1], Colorstate = [1 0 1 0 0 0 0 0]
Marblestate = [3 2], Colorstate = [0 1 1 0 0 0 0 0]
Marblestate = [3 3], Colorstate = [0 0 2 0 0 0 0 0]
Marblestate = [3 4], Colorstate = [0 0 1 1 0 0 0 0]
Marblestate = [3 5], Colorstate = [0 0 1 0 1 0 0 0]
Marblestate = [3 6], Colorstate = [0 0 1 0 0 1 0 0]
Marblestate = [3 7], Colorstate = [0 0 1 0 0 0 1 0]
Marblestate = [3 8], Colorstate = [0 0 1 0 0 0 0 1]
Marblestate = [4 1], Colorstate = [1 0 0 1 0 0 0 0]
Marblestate = [4 2], Colorstate = [0 1 0 1 0 0 0 0]
Marblestate = [4 3], Colorstate = [0 0 1 1 0 0 0 0]
Marblestate = [4 4], Colorstate = [0 0 0 2 0 0 0 0]
Marblestate = [4 5], Colorstate = [0 0 0 1 1 0 0 0]
Marblestate = [4 6], Colorstate = [0 0 0 1 0 1 0 0]
Marblestate = [4 7], Colorstate = [0 0 0 1 0 0 1 0]
Marblestate = [4 8], Colorstate = [0 0 0 1 0 0 0 1]
Marblestate = [5 1], Colorstate = [1 0 0 0 1 0 0 0]
Marblestate = [5 2], Colorstate = [0 1 0 0 1 0 0 0]
Marblestate = [5 3], Colorstate = [0 0 1 0 1 0 0 0]
Marblestate = [5 4], Colorstate = [0 0 0 1 1 0 0 0]
Marblestate = [5 5], Colorstate = [0 0 0 0 2 0 0 0]
Marblestate = [5 6], Colorstate = [0 0 0 0 1 1 0 0]
Marblestate = [5 7], Colorstate = [0 0 0 0 1 0 1 0]
Marblestate = [5 8], Colorstate = [0 0 0 0 1 0 0 1]
Marblestate = [6 1], Colorstate = [1 0 0 0 0 1 0 0]
Marblestate = [6 2], Colorstate = [0 1 0 0 0 1 0 0]
Marblestate = [6 3], Colorstate = [0 0 1 0 0 1 0 0]
Marblestate = [6 4], Colorstate = [0 0 0 1 0 1 0 0]
Marblestate = [6 5], Colorstate = [0 0 0 0 1 1 0 0]
Marblestate = [6 6], Colorstate = [0 0 0 0 0 2 0 0]
Marblestate = [6 7], Colorstate = [0 0 0 0 0 1 1 0]
Marblestate = [6 8], Colorstate = [0 0 0 0 0 1 0 1]
Marblestate = [7 1], Colorstate = [1 0 0 0 0 0 1 0]
Marblestate = [7 2], Colorstate = [0 1 0 0 0 0 1 0]
Marblestate = [7 3], Colorstate = [0 0 1 0 0 0 1 0]
Marblestate = [7 4], Colorstate = [0 0 0 1 0 0 1 0]
Marblestate = [7 5], Colorstate = [0 0 0 0 1 0 1 0]
Marblestate = [7 6], Colorstate = [0 0 0 0 0 1 1 0]
Marblestate = [7 7], Colorstate = [0 0 0 0 0 0 2 0]
Marblestate = [7 8], Colorstate = [0 0 0 0 0 0 1 1]
Marblestate = [8 1], Colorstate = [1 0 0 0 0 0 0 1]
Marblestate = [8 2], Colorstate = [0 1 0 0 0 0 0 1]
Marblestate = [8 3], Colorstate = [0 0 1 0 0 0 0 1]
Marblestate = [8 4], Colorstate = [0 0 0 1 0 0 0 1]
Marblestate = [8 5], Colorstate = [0 0 0 0 1 0 0 1]
Marblestate = [8 6], Colorstate = [0 0 0 0 0 1 0 1]
Marblestate = [8 7], Colorstate = [0 0 0 0 0 0 1 1]
Marblestate = [8 8], Colorstate = [0 0 0 0 0 0 0 2]