sum of kronecker products(four loops)

3 次查看(过去 30 天)
Hello! I would like to know if the following code:
function result = kron_test(var1,var2,var3,var4)
result = zeros(N^2,N^2);
for e=1:N
for f=1:N
for g=1:N
for h=1:N
result = result + kron(var1(:,:,e,g),var2(:,:,f,h))*var3(e,f)*var4(g,h);
end
end
end
end
end
and the following:
function result = kron_test(var1,var2,var3,var4)
Var1 = reshape(var1,N^2,N^2);
Var2 = reshape(var2,N^2,N^2);
tmp = num2cell( reshape( Var2*kron(var4,var3).'*Var1.', N, N, [] ), [1,2] );
result = cell2mat(reshape(tmp,N,N));
end
are the same. I need to use this code for N > 30.
Thank you.

回答(1 个)

Matt J
Matt J 2021-5-10
For me, the following test for N=20 gives a very low percent error, so I would bet that the two are equivalent.
N=20;
[var1,var2]=deal( rand(N,N,N,N));
[var3,var4]=deal( rand(N,N) );
d=norm(kron_test0(var1,var2,var3,var4)-kron_test(var1,var2,var3,var4),'inf')
d0=norm(kron_test(var1,var2,var3,var4),'inf');
percentError = d/d0*100 % 9.7209e-13

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by