MATLAB之——Kronecker积
所谓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)
MATLAB代码
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
MATLAB代码
- function K = kron(A,B)
- %KRON Kronecker tensor product.
- % KRON(X,Y) is the Kronecker tensor product of X and Y.
- % The result is a large matrix formed by taking all possible
- % products between the elements of X and those of Y. For
- % example, if X is 2 by 3, then KRON(X,Y) is
- %
- % [ X(1,1)*Y X(1,2)*Y X(1,3)*Y
- % X(2,1)*Y X(2,2)*Y X(2,3)*Y ]
- %
- % If either X or Y is sparse, only nonzero elements are multiplied
- % in the computation, and the result is sparse.
- %
- % Class support for inputs X,Y:
- % float: double, single
- % Previous versions by Paul Fackler, North Carolina State,
- % and Jordan Rosenthal, Georgia Tech.
- % Copyright 1984-2004 The MathWorks, Inc.
- % $Revision: 5.17.4.2 $ $Date: 2004/06/25 18:52:18 $
- [ma,na] = size(A);
- [mb,nb] = size(B);
- if ~issparse(A) && ~issparse(B)
- % Both inputs full, result is full.
- [ia,ib] = meshgrid(1:ma,1:mb);
- [ja,jb] = meshgrid(1:na,1:nb);
- K = A(ia,ja).*B(ib,jb);
- else
- % At least one input is sparse, result is sparse.
- [ia,ja,sa] = find(A);
- [ib,jb,sb] = find(B);
- ia = ia(:); ja = ja(:); sa = sa(:);
- ib = ib(:); jb = jb(:); sb = sb(:);
- ka = ones(size(sa));
- kb = ones(size(sb));
- t = mb*(ia-1)";
- ik = t(kb,:)+ib(:,ka);
- t = nb*(ja-1)";
- jk = t(kb,:)+jb(:,ka);
- K = sparse(ik,jk,sb*sa.",ma*mb,na*nb);
- end
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 网页变量 & JavaScript 伪协议
- 下一篇: matlab实现形态学处理