我认为这更容易是您考虑弹珠的状态,而不是颜色的状态。然后根据需要从大理石状态转换为颜色状态。例如,如果考虑所有可能的弹珠状态的有序列表,您可以创建如下所示的函数(使用全零作为“全部完成”标志):
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]