sum of kronecker products(four loops)

2 views (last 30 days)
Mazdack Ameri
Mazdack Ameri on 9 May 2021
Answered: Matt J on 10 May 2021
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.

Answers (1)

Matt J
Matt J on 10 May 2021
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

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!