繰り返す方法
A = [ 1 2 ;
3 4 ]
によって繰り返される
B = [ 1 2 ;
2 1 ]
だから私はマトリックスCのような私の答えが欲しいです:
C = [ 1 2 2;
3 3 4 ]
ご協力いただきありがとうございます。
それを楽しむために、arrayfunを利用する別のソリューション:
res = cell2mat(arrayfun(@(a,b) ones(b,1).*a, A', B', 'uniformoutput', false))'
これにより、次のようになります。
res =
1 2 2
3 3 4
これを簡単にするために、列を追加するだけで、各行に同じ数の列があることを確認したと仮定します。
次に、要素の繰り返しと再形成の単純な組み合わせになります。
編集AとBが3D配列の場合にも機能するように、コードを変更しました。
%# get the number of rows from A, transpose both
%# A and B so that linear indexing works
[nRowsA,~,nValsA] = size(A);
A = permute(A,[2 1 3]);
B = permute(B,[2 1 3]);
%# create an index vector from B
%# so that we know what to repeat
nRep = sum(B(:));
repIdx = zeros(1,nRep);
repIdxIdx = cumsum([1 B(1:end-1)]);
repIdx(repIdxIdx) = 1;
repIdx = cumsum(repIdx);
%# assemble the array C
C = A(repIdx);
C = permute(reshape(C,[],nRowsA,nValsA),[2 1 3]);
C =
1 2 2
3 3 4