How to repeat
A = [ 1 2 ; 
      3 4 ]
repeated by
B = [ 1 2 ; 
      2 1 ]
So I want my answer like matrix C:
C = [ 1 2 2; 
      3 3 4 ]
Thanks for your help.
How to repeat
A = [ 1 2 ; 
      3 4 ]
repeated by
B = [ 1 2 ; 
      2 1 ]
So I want my answer like matrix C:
C = [ 1 2 2; 
      3 3 4 ]
Thanks for your help.
 
    
     
    
    Just for the fun of it, another solution making use of arrayfun:
res = cell2mat(arrayfun(@(a,b) ones(b,1).*a, A', B', 'uniformoutput', false))'
This results in:
res =
     1     2     2
     3     3     4
 
    
    To make this simple, I assume that you're only going to add more columns, and that you've checked that you have the same number of columns for each row.
Then it becomes a simple combination of repeating elements and reshaping.
EDIT I've modified the code so that it also works if A and B are 3D arrays.
%# 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
