File Exchange

image thumbnail

Kronecker product

version (1.46 KB) by Laurent S
An efficient implementation of the Kronecker product for dense, sparse and logical matrices.


Updated 06 Feb 2011

View License

This file serves as a replacement for Matlab's kron.m. It contains a more efficient implementation of the Kronecker product for dense, sparse and logical matrices.

Cite As

Laurent S (2019). Kronecker product (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

This function was not efficient for my run test. According to the profiler the matlab function kron took 34s while this one took 64s. Still looking for a better option than the current matlab function though.


Works great now! Thanks a lot for the fix and the update.

Laurent S

@Paulo,Matt J: Thanks for letting me know, I submitted an updated version.

Matt J

These two modified lines of code are needed:

[ia,ja,sa] = find(A); ia=ia(:); ja=ja(:); sa=sa(:);
[ib,jb,sb] = find(B); ib=ib(:); jb=jb(:); sb=sb(:);


kron(1:3, speye(3)) does not work (Matlab 2010b 64bit Linux).

Here's the error message:

??? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> kron at 24
X = sparse(ix,jx,double(sb)*double(sa.'),I*K,J*L);

Laurent S

@Matt J: I was under the impression converting sa and sb to doubles would be slower than converting their outer product to double, unfortunately Matlab isn't too good about handling operations between different types of data. I updated the code again.

Matt J

I'm going to stop bothering you, but my recommended solution also handled integer types, whereas, your current one does not.

??? Error using ==> mtimes
Integers can only be combined with integers of the same class, or scalar doubles.

Is there a reason you're trying so hard to economize on if-statements and lines of code?

Matt J

Laurent S

@Matt J: I updated the code again to include support for other class types, thanks for catching that.

Matt J

Nice 1-line trick, but it prevents you from having full class support when you're already so close!

Notice that mixed sparse/single ops still don't work

>> kron(sparse(rand(6)),rand(6,'single'));
??? Undefined function or method 'sparse' for input arguments of type 'single'.

This is my revised recommendation

if islogical(sb) && islogical(sa)
X = sparse(ix,jx,bsxfun(@and,sb,sa.'),I*K,J*L);

if ~(isdouble(sa)||islogical(sa)); sa=double(sa); end
if ~(isdouble(sb)||islogical(sb)); sb=double(sb); end

X = sparse(ix,jx,sb*sa.',I*K,J*L);

Laurent S

@Matt J: The updated version supports logical matrices (adding only one line of code), thanks for your comment!

Matt J

Very good, but like the original kron, this version cannot handle logical inputs, e.g.

??? Error using ==> mtimes
Logical inputs must be scalar.

Error in ==> kron at 25
X = sparse(ix,jx,sb*sa.',I*K,J*L);

Consider making the following modification

if islogical(sb) && islogical(sa)
X = sparse(ix,jx,bsxfun(@and,sb,sa.'),I*K,J*L);
elseif islogical(sb)
elseif islogical(sa)
X = sparse(ix,jx,sb*sa.',I*K,J*L);


As promised, the program does the job faster than the original implementation.
Very good!


Fixed a bug where an error would occur if one of the arguments is a row vector (thanks Paulo and Matt J).

Improved support for combinations of different data types.

Included support for more class types, such as single precision.

Included support for logical matrices.

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Inspired by: kronecker