Help to create a loop

조회 수: 2 (최근 30일)
Luciano Campos
Luciano Campos 2016년 3월 14일
댓글: Luciano Campos 2016년 3월 14일
Hi everybody, Can anybody help me to write a loop that achieves the same result as this code?:
%
G(:,:,1)=kron(J*A'^0,Phi(:,:,1));
G(:,:,2)=kron(J*A'^1,Phi(:,:,1))+kron(J*A'^0,Phi(:,:,2));
G(:,:,3)=kron(J*A'^2,Phi(:,:,1))+kron(J*A'^1,Phi(:,:,2))+kron(J*A'^0,Phi(:,:,3));
G(:,:,4)=kron(J*A'^3,Phi(:,:,1))+kron(J*A'^2,Phi(:,:,2))+kron(J*A'^1,Phi(:,:,3))+kron(J*A'^0,Phi(:,:,4));
%
and so on, until G(:,:,9). Where
%
J=[eye(3) zeros(3)]
%
A =
-0.32 0.15 0.96 -0.16 0.15 0.93
0.04 -0.15 0.29 0.05 0.02 -0.01
-0.00 0.22 -0.26 0.03 0.35 -0.02
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
and Phi is in attached.
Thanks for your help
best,

채택된 답변

Matthew Eicholtz
Matthew Eicholtz 2016년 3월 14일
Here is one potential approach:
G = zeros(9,18,9); %seems to me to be the size you need to preallocate
for ii=1:9
for jj=ii-1:-1:0
G(:,:,ii) = G(:,:,ii) + kron(J*A'^jj, Phi(:,:,ii-jj));
end
end
  댓글 수: 1
Luciano Campos
Luciano Campos 2016년 3월 14일
Thanks Matt! It Works!
best,

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2016년 3월 14일
I don't see where the difficulty is:
for page = 1:9
gsum = [];
for iter = 1:page
gsum = gsum + kron(J*A'^(page-iter), Phi(:,:,iter));
end
G(:, :, page) = gsum;
end
There may be a way to vectorise the inner loop but I've not tried to understand what the code is doing.
  댓글 수: 1
Luciano Campos
Luciano Campos 2016년 3월 14일
Thanks for your help, Guillaume.
best,

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by