別の解決策:
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
説明:
repmat(0:B(ii),1,prod(B(1:(ii-1))+1)
繰り返し0:B(ii)
何度でもその前にBさんの全ての要素の積としてシーケンス。カウントがゼロから始まることを考慮に入れるために、すべての要素に+1
を追加します。
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
:あなたは、画像処理ツールボックスを持っていない場合は
interp1
の代わり
imresize
を使用して
バージョン:
imresize(...,[1 L], 'nearest')';
は、アレイ
編集の長さに、その後のベクトルをスケーリングします
私があなたの質問を正しく理解していれば、[こちら](https://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from)のアプローチを使用することができます'-n-vectors/21895344#21895344)は' vectors = cell(1、numel(B));を定義します。 k = 1の場合:numel(B)、ベクトル{k} = 1:B(k)。 end' –