sum of kronecker products(four loops)
1 view (last 30 days)
Show older comments
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 Comment
Answers (1)
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
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!