Fast, sparse kronecker product with identity in MATLAB

Alec Jacobson

July 06, 2012

weblog/

I needed to compute:
  K = kron(A,speye(n,n));
but was a little dismayed at the speed. Here's how I get about a 6x speed up:
  % Compute kron(speye(n,n),A) in a fast way       
  C = cell(n,1);                                   
  [C{:}] = deal(A);                                
  K = blkdiag(C{:});                               
  % Rearrange rows and columns                     
  I = reshape(reshape(1:(size(A,1)*n),size(A,1),n)',size(A,1)*n,1);
  J = reshape(reshape(1:(size(A,2)*n),size(A,2),n)',size(A,2)*n,1);
  K = K(I,J);