Matlab 两张量之间的Kronecker积

Matlab 两张量之间的Kronecker积,matlab,matrix,vectorization,multiplication,Matlab,Matrix,Vectorization,Multiplication,我有两个张量:x是2×2×3,y也是2×2×3。定义张量的每个正面切片为x1x2x3,y1,y2,y3。席或彝是2×2矩阵。如何在matlab中实现x和y之间的kronecker乘积?我想要得到的是matlab中的kron(x1,y1),kron(x2,y2),kron(x3,y3),没有任何循环。这适用于任意大小的x和y: 构建一个5D阵列z。前两个维度包含x和y行组合的乘积;接下来的两个包含x和y列组合的乘积;第五个是原始的三维空间 该阵列将被重塑,一方面折叠第一个和第二个维度,另一方面折叠

我有两个张量:x是2×2×3,y也是2×2×3。定义张量的每个正面切片为x1x2x3,y1,y2,y3。席或彝是2×2矩阵。如何在matlab中实现x和y之间的kronecker乘积?我想要得到的是matlab中的kron(x1,y1),kron(x2,y2),kron(x3,y3),没有任何循环。

这适用于任意大小的
x
y

  • 构建一个5D阵列
    z
    。前两个维度包含
    x
    y
    行组合的乘积;接下来的两个包含
    x
    y
    列组合的乘积;第五个是原始的三维空间
  • 该阵列将被重塑,一方面折叠第一个和第二个维度,另一方面折叠第三个和第四个维度,以生成最终结果:
  • 代码:


    这可能是一种方法-

    %// Pre-processing part
    [m,n,r] = size(x) %// Get size
    N = m*n %// number of elements in one 3D slice
    
    %// ------------- PART 1: Get indices for each 3D slice
    
    %// Get the first mxm block of kron-corresponding indices and then add to
    %// each such block for the indices corresponding to the kron multiplications
    %// of each iteration
    a1 = bsxfun(@plus,reshape([0:N-1]*N+1,m,m),permute([0:N-1],[1 3 2]))
    
    %// Now, a1 is a 3D array, we need to make 2D array out of it.
    %// So, concatenate along rows to make it a "slimish" 2D array
    a2 = reshape(permute(a1,[1 3 2]),size(a1,1)*size(a1,3),[])
    
    %// Cut after every N rows to make it a square 2D array.
    %// These are the indices for each frontal tensor of kron muliplications
    slice_idx = reshape(permute(reshape(a2,N,size(a2,1)/N,[]),[1 3 2]),N,N)
    
    %// -------------  PART 2: Get kron equivalent output
    
    %// Perform x:(Nx1) x y:(1xN) multiplications
    vals = bsxfun(@times,reshape(x,m*n,1,r),reshape(y,1,m*n,r)) %//multiplications
    
    %// Get indices for all 3D slices and then index into those multiplications
    %// with these for the final kron equivalent output
    all_idx=bsxfun(@plus,slice_idx,permute([0:r-1]*m*m*n*n,[1 3 2])) %//all indices
    out = vals(all_idx) %// final output of kron equivalent multiplications
    

    因此,在MATLAB中,您需要高效地找到一种方法来调用
    kron
    三次:
    kron(x1,y1);克朗(x2,y2);克朗(x3,y3)对于来自每个张量的每对2D切片
    kron
    只能接受2D矩阵-我不知道如何在没有任何循环的情况下将其矢量化。为什么转义循环对应用程序如此重要。。。尤其是当张量很小的时候?哦,它只是3维!那么我对你的代码就更不了解了:-)@LuisMendo是的,有很多事情在发生!添加评论:)@迪瓦卡。这真是太神奇了。。。。。你怎么会对matlab中的bxsfun()如此熟悉?。。。。。。谢谢Divakar。@meng对它上瘾了,康复不会带我去的!;)@迪瓦卡。我会仔细阅读你的代码,并在下次尝试自己解决这种索引问题。哇,真的很好!紧凑的代码!您需要将
    2*m
    更改为
    m^2
    ,但
    n
    也需要更改。嘿,谢谢!更正。我想这是我第一次在5D中使用
    bsxfun
    :)通常情况下,3D已经足够了,恭喜太有趣了!!对绝对是最有趣的功能!
    %// Pre-processing part
    [m,n,r] = size(x) %// Get size
    N = m*n %// number of elements in one 3D slice
    
    %// ------------- PART 1: Get indices for each 3D slice
    
    %// Get the first mxm block of kron-corresponding indices and then add to
    %// each such block for the indices corresponding to the kron multiplications
    %// of each iteration
    a1 = bsxfun(@plus,reshape([0:N-1]*N+1,m,m),permute([0:N-1],[1 3 2]))
    
    %// Now, a1 is a 3D array, we need to make 2D array out of it.
    %// So, concatenate along rows to make it a "slimish" 2D array
    a2 = reshape(permute(a1,[1 3 2]),size(a1,1)*size(a1,3),[])
    
    %// Cut after every N rows to make it a square 2D array.
    %// These are the indices for each frontal tensor of kron muliplications
    slice_idx = reshape(permute(reshape(a2,N,size(a2,1)/N,[]),[1 3 2]),N,N)
    
    %// -------------  PART 2: Get kron equivalent output
    
    %// Perform x:(Nx1) x y:(1xN) multiplications
    vals = bsxfun(@times,reshape(x,m*n,1,r),reshape(y,1,m*n,r)) %//multiplications
    
    %// Get indices for all 3D slices and then index into those multiplications
    %// with these for the final kron equivalent output
    all_idx=bsxfun(@plus,slice_idx,permute([0:r-1]*m*m*n*n,[1 3 2])) %//all indices
    out = vals(all_idx) %// final output of kron equivalent multiplications