牛骨文教育服务平台(让学习变的简单)
博文笔记

MATLAB之——Kronecker积

创建时间:2018-01-12 投稿人: 浏览次数:827
所谓Kronecker积是一种矩阵运算,其定义可以简单描述成:X与Y的Kronecker积的结果是一个矩阵: X11*Y   X12*Y … X1n*Y
X21*Y   X22*Y … X2n*Y
……
Xm1*Y   Xm2*Y … Xmn*Y
Kron函数与repat函数的区别,
B = repmat(A,m,n)

这是一个处理大矩阵且内容有重复时使用,其功能是以A的内容堆叠在(MxN)的矩阵B中,B矩阵的大小由MxN及A矩阵的内容决定,如果A是一个3x4x5的矩阵,有B = repmat(A,2,3)则最后的矩阵是6x12x5

例如:
B=repmat( [1 2;3 4],2,3)
B = 

1      2      1     2    1    2

3      4      3     4    3    4

1     2     1     2     1     2

3     4     3     4     3     4

Kron可以实现repmat功能。比如: kron(ones(2,3),[1 2;3 4])就可以实现以上功能
MATLAB代码
  1. function K = kron(A,B)  
  2. %KRON   Kronecker tensor product.  
  3. %   KRON(X,Y) is the Kronecker tensor product of X and Y.  
  4. %   The result is a large matrix formed by taking all possible  
  5. %   products between the elements of X and those of Y.   For  
  6. %   example, if X is 2 by 3, then KRON(X,Y) is  
  7. %  
  8. %      [ X(1,1)*Y  X(1,2)*Y  X(1,3)*Y  
  9. %        X(2,1)*Y  X(2,2)*Y  X(2,3)*Y ]  
  10. %  
  11. %   If either X or Y is sparse, only nonzero elements are multiplied  
  12. %   in the computation, and the result is sparse.  
  13. %  
  14. %   Class support for inputs X,Y:  
  15. %      float: double, single   
  16.   
  17. %   Previous versions by Paul Fackler, North Carolina State,  
  18. %   and Jordan Rosenthal, Georgia Tech.  
  19. %   Copyright 1984-2004 The MathWorks, Inc.   
  20. %   $Revision: 5.17.4.2 $ $Date: 2004/06/25 18:52:18 $   
  21.   
  22. [ma,na] = size(A);  
  23. [mb,nb] = size(B);   
  24.   
  25. if ~issparse(A) && ~issparse(B)   
  26.   
  27.    % Both inputs full, result is full.   
  28.   
  29.    [ia,ib] = meshgrid(1:ma,1:mb);  
  30.    [ja,jb] = meshgrid(1:na,1:nb);  
  31.    K = A(ia,ja).*B(ib,jb);   
  32.   
  33. else   
  34.   
  35.    % At least one input is sparse, result is sparse.   
  36.   
  37.    [ia,ja,sa] = find(A);  
  38.    [ib,jb,sb] = find(B);  
  39.    ia = ia(:); ja = ja(:); sa = sa(:);  
  40.    ib = ib(:); jb = jb(:); sb = sb(:);  
  41.    ka = ones(size(sa));  
  42.    kb = ones(size(sb));  
  43.    t = mb*(ia-1)";  
  44.    ik = t(kb,:)+ib(:,ka);  
  45.    t = nb*(ja-1)";  
  46.    jk = t(kb,:)+jb(:,ka);  
  47.    K = sparse(ik,jk,sb*sa.",ma*mb,na*nb);   
  48.   
  49. end  
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。