sum of kronecker products(four loops)

2 ビュー (過去 30 日間)
Mazdack Ameri
Mazdack Ameri 2021 年 5 月 9 日
回答済み: Matt J 2021 年 5 月 10 日
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 5 月 9 日
At least for the N = 3 and N = 4 cases, it checks out.

サインインしてコメントする。

回答 (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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by