jam*_*orm 2 matlab loops combinatorics
假设我有大理石,每种都可以是8种颜色中的一种.我想迭代所有可能的大理石颜色组合值.我怎样才能在MatLab中做到这一点?
例如,如果n = 2,那么我想迭代:
2 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 1 0 0 0 0 0
1 0 0 1 0 0 0 0
1 0 0 0 1 0 0 0
1 0 0 0 0 1 0 0
1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 1
0 2 0 0 0 0 0 0
0 1 1 0 0 0 0 0
0 1 0 1 0 0 0 0
等等
编辑:
这是一些伪代码的样子,但你可以看到它非常草率.我对这样做更简洁的方式感兴趣,并且不需要if语句......
for i8 = 0:1:n
for i7 = 0:1:n - i8
for i6 = 0:1:n - i8 - i7
for i5 = 0:1:n - i8 - i7 - i6
for i4 = 0:1:n - i8 - i7 - i6 - i5
for i3 = 0:1:n - i8 - i7 - i6 - i5 - i4
for i2 = 0:1:n - i8 - i7 - i6 - i5 - i4 - i3
for i1 = 0:1:n - i8 - i7 - i6 - i5 - i4 - i3 - i2
if i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 == n
i = [i1, i2, i3, i4, i5, i6, i7, i8]
end
end
end
end
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是解决方案(代码在GNU/Octave中测试,它也应该在Matlab中工作).此代码派生自Octave-Forge multinom_exp.m
function conf = marbin (nmar,nbin)
%% Returns the position of nmar in nbis, allowing the marbles to be in the same bin.
%% This is standard stars and bars.
numsymbols = nbin+nmar-1;
stars = nchoosek (1:numsymbols, nmar);
%% Star labels minus their consecutive position becomes their index
%% position!
idx = bsxfun (@minus, stars, [0:nmar-1]);
%% Manipulate indices into the proper shape for accumarray.
nr = size (idx, 1);
a = repmat ([1:nr], nmar, 1);
b = idx';
conf = [a(:), b(:)];
conf = accumarray (conf, 1);
Run Code Online (Sandbox Code Playgroud)