两个张量之间的克罗内克积
Kronecker product between two tensors
我有两个张量:x 是 2×2×3,y 也是 2×2×3。定义张量的每个正面切片为 x1 x2 x3,y1,y2,y3。 xi 或 yi 是 2×2 矩阵。我怎样才能在 matlab 中做 x 和 y 之间的克罗内克积?我想得到的是 kron(x1,y1),kron(x2,y2),kron(x3,y3) 同时在 matlab 中没有任何循环。
这适用于 x
和 y
的任意大小:
- 构建一个 5 维数组
z
。前两个维度包含 x
和 y
行组合的乘积;接下来的两个包含 x
和 y
列组合的乘积;第五个是原来的三维
- 该数组被重塑,一方面折叠第一个和第二个维度,另一方面折叠第三个和第四个维度,以产生最终结果:
代码:
z = bsxfun(@times, permute(x, [4 1 5 2 3]), permute(y, [1 4 2 5 3])); %// step 1
z = reshape(z, size(x,1)^2, size(x,2)^2, size(x,3)); %//step 2
这可能是一种方法 -
%// 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
我有两个张量:x 是 2×2×3,y 也是 2×2×3。定义张量的每个正面切片为 x1 x2 x3,y1,y2,y3。 xi 或 yi 是 2×2 矩阵。我怎样才能在 matlab 中做 x 和 y 之间的克罗内克积?我想得到的是 kron(x1,y1),kron(x2,y2),kron(x3,y3) 同时在 matlab 中没有任何循环。
这适用于 x
和 y
的任意大小:
- 构建一个 5 维数组
z
。前两个维度包含x
和y
行组合的乘积;接下来的两个包含x
和y
列组合的乘积;第五个是原来的三维 - 该数组被重塑,一方面折叠第一个和第二个维度,另一方面折叠第三个和第四个维度,以产生最终结果:
代码:
z = bsxfun(@times, permute(x, [4 1 5 2 3]), permute(y, [1 4 2 5 3])); %// step 1
z = reshape(z, size(x,1)^2, size(x,2)^2, size(x,3)); %//step 2
这可能是一种方法 -
%// 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