Eine andere Lösung:
L = prod(B+1); % length of the resulting array
output = zeros(L,length(B)); % preallocating memory
for ii = 1:length(B) % for each column of the output array
output(:,ii) = imresize(repmat(0:B(ii),1,prod(B(1:(ii-1))+1)), [1 L], 'nearest')';
end
Erläuterung:
repmat(0:B(ii),1,prod(B(1:(ii-1))+1)
Wiederholt die Sequenz 0:B(ii)
so oft wie das Produkt aller Elemente von B vor sich her. Um zu berücksichtigen, dass die Zählung bei Null beginnt, fügen wir allen Elementen +1
hinzu.
imresize(...,[1 L], 'nearest')';
Scales den Vektor dann auf die Länge des Arrays
Edit:
Version interp1
statt imresize
im Falle verwenden Sie die Bildverarbeitung Toolbox nicht haben:
L = prod(B+1); % length of the resulting array
output = zeros(L,length(B)); % preallocating memory
for ii = 1:length(B) % for each column of the output array
p = prod(B(1:(ii-1))+1);
output(:,ii) = interp1(1:(((B(ii)+1)*p)), repmat(0:B(ii),1,p), linspace(1, (((B(ii)+1)*p)), L), 'nearest');
end
Wenn ich Ihre Frage richtig verstanden habe, können Sie den Ansatz verwenden [hier] (https://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from -n-Vektoren/21895344 # 21895344) Definition von Vektoren = Zelle (1, Anzahl (B)); für k = 1: Zahl (B), Vektoren {k} = 1: B (k); end' –