Tuesday, 31 January 2017

I wish I knew MATLAB had... kron


    • The function kron creates the Kronecker (tensor) product of two arrays. Why on earth would this ever be useful?
    • You can use it instead of repmat for certain tasks. Let's say you have an array 
      x = [1,3,5,7]
    • What if you now wanted to triplicate each element, to get [1,1,1,3,3,3,5,5,5,7,7,7]? You might try repmat:
      y = repmat(x,3,1);   y=y(:);
    • which simply creates three rows and then "flattens" the matrix to a vector. But using kron can be simpler:
      y = kron(x,[1 1 1])
    • This replaces each element of y with y(i) * [1 1 1], giving the desired 9 elements.

    No comments:

    Post a Comment