MATLAB: Repmat with pattern

diagonalindexingrepmat

Does anyone know an efficient way of replicating a matrix in a predetermined pattern(such as diagonal or triangular)?
Some examples: 1. We have A=[1,2;3,4] and we want B=[A,zeros(2);zeros(2),A] 2. We have A=[1,2;3,4] and we want B=[A,zeros(2);A,A] 3. We have a=[1;2;3;4] and we want B=[a,zeros(4,1);zeros(4,1),a]

Best Answer

  1. kron([1 0;0 1],A)
  2. kron([1 0;1 0],A)
  3. kron([1 0 0;0 0 1],A)
That is, put a 1 for each copy of A that should appear, and put a 0 for each place 0's the same size as A should appear.