フィルターのクリア

Kronocker product of iteration sequence

3 ビュー (過去 30 日間)
Akmyrat
Akmyrat 2014 年 7 月 4 日
編集済み: Matt J 2023 年 12 月 20 日
I have this code:
A=[1 0 0;1 1 0;0 1 0]
I=[1 0;0 1]
F2=[2 1;1 2]
for i=1:3
B=0;
for j=1:3
B=B+A(i,j);
end
if B==1;
eval(sprintf('R%d=I',i))
else B==2;
eval(sprintf('R%d=F2',i))
end
end
%% At the end I want to multiply all R1,R2,R3 answers like this: A=kron(R1,R2), B=kron(A,R3)
urgently need answer...thankssss

採用された回答

Voss
Voss 2023 年 12 月 20 日
編集済み: Voss 2023 年 12 月 20 日
A=[1 0 0;1 1 0;0 1 0];
I=[1 0;0 1];
F2=[2 1;1 2];
Instead of using numbered variables R1, R2, R3, use a cell array:
[N1,N2] = size(A);
R = cell(1,N1); % 1xN1 cell array
for ii = 1:N1
B=0;
for jj=1:N2
B=B+A(ii,jj);
end
if B==1
R{ii} = I;
else
R{ii} = F2;
end
end
R{:}
ans = 2×2
1 0 0 1
ans = 2×2
2 1 1 2
ans = 2×2
1 0 0 1
And you can avoid the inner loop by using the sum function:
B = sum(A,2);
N = size(A,1);
R = cell(1,N);
for ii = 1:N
if B(ii) == 1
R{ii} = I;
else
R{ii} = F2;
end
end
R{:}
ans = 2×2
1 0 0 1
ans = 2×2
2 1 1 2
ans = 2×2
1 0 0 1
And you can avoid the outer loop by using logical indexing:
R = cell(1,size(A,1));
idx = sum(A,2) == 1;
R(idx) = {I};
R(~idx) = {F2};
R{:}
ans = 2×2
1 0 0 1
ans = 2×2
2 1 1 2
ans = 2×2
1 0 0 1
"At the end I want to multiply all R1,R2,R3 answers like this: A=kron(R1,R2), B=kron(A,R3)"
No problem; you can do that:
A=kron(R{1},R{2}); B=kron(A,R{3})
B = 8×8
2 0 1 0 0 0 0 0 0 2 0 1 0 0 0 0 1 0 2 0 0 0 0 0 0 1 0 2 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 2 0 1 0 0 0 0 1 0 2 0 0 0 0 0 0 1 0 2
A more general way:
B = R{1};
for ii = 2:numel(R)
B = kron(B,R{ii});
end
B
B = 8×8
2 0 1 0 0 0 0 0 0 2 0 1 0 0 0 0 1 0 2 0 0 0 0 0 0 1 0 2 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 2 0 1 0 0 0 0 1 0 2 0 0 0 0 0 0 1 0 2
  1 件のコメント
Matt J
Matt J 2023 年 12 月 20 日
編集済み: Matt J 2023 年 12 月 20 日
A more general way:
You can do this even more simply by downloading,
B=full(KronProd(flip(R)))

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by