# summation inside for loop

2 views (last 30 days)
Mert Demir on 21 Jul 2021
Edited: Mert Demir on 4 Nov 2021
Have a good day. I am trying to find an easy way of writing a loop below:
for iz=1:5
for ie=1:5
for iznext=1
for ienext=1
V_W(:,iz,ie)=
v_w(:,iznext,ienext)*Q_e(ie,ienext)*Q_z(iz,iznext)
+v_w(:,iznext+1,ienext)*Q_e(ie,ienext)*Q_z(iz,iznext+1)
+v_w(:,iznext+2,ienext)*Q_e(ie,ienext)*Q_z(iz,iznext+2)
+v_w(:,iznext+3,ienext)*Q_e(ie,ienext)*Q_z(iz,iznext+3)
+v_w(:,iznext+4,ienext)*Q_e(ie,ienext)*Q_z(iz,iznext+4)
+v_w(:,iznext,ienext+1)*Q_e(ie,ienext+1)*Q_z(iz,iznext)
+v_w(:,iznext+1,ienext+1)*Q_e(ie,ienext+1)*Q_z(iz,iznext+1)
+v_w(:,iznext+2,ienext+1)*Q_e(ie,ienext+1)*Q_z(iz,iznext+2)
+v_w(:,iznext+3,ienext+1)*Q_e(ie,ienext+1)*Q_z(iz,iznext+3)
+v_w(:,iznext+4,ienext+1)*Q_e(ie,ienext+1)*Q_z(iz,iznext+4)
+v_w(:,iznext,ienext+2)*Q_e(ie,ienext+2)*Q_z(iz,iznext)
+v_w(:,iznext+1,ienext+2)*Q_e(ie,ienext+2)*Q_z(iz,iznext+1)
+v_w(:,iznext+2,ienext+2)*Q_e(ie,ienext+2)*Q_z(iz,iznext+2)
+v_w(:,iznext+3,ienext+2)*Q_e(ie,ienext+2)*Q_z(iz,iznext+3)
+v_w(:,iznext+4,ienext+2)*Q_e(ie,ienext+2)*Q_z(iz,iznext+4)
+v_w(:,iznext,ienext+3)*Q_e(ie,ienext+3)*Q_z(iz,iznext)
+v_w(:,iznext+1,ienext+3)*Q_e(ie,ienext+3)*Q_z(iz,iznext+1)
+v_w(:,iznext+2,ienext+3)*Q_e(ie,ienext+3)*Q_z(iz,iznext+2)
+v_w(:,iznext+3,ienext+3)*Q_e(ie,ienext+3)*Q_z(iz,iznext+3)
+v_w(:,iznext+4,ienext+3)*Q_e(ie,ienext+3)*Q_z(iz,iznext+4)
+v_w(:,iznext,ienext+4)*Q_e(ie,ienext+4)*Q_z(iz,iznext)
+v_w(:,iznext+1,ienext+4)*Q_e(ie,ienext+4)*Q_z(iz,iznext+1)
+v_w(:,iznext+2,ienext+4)*Q_e(ie,ienext+4)*Q_z(iz,iznext+2)
+v_w(:,iznext+3,ienext+4)*Q_e(ie,ienext+4)*Q_z(iz,iznext+3)
+v_w(:,iznext+4,ienext+4)*Q_e(ie,ienext+4)*Q_z(iz,iznext+4) ;

VBBV on 21 Jul 2021
%if true
for iznext=1:5
Change this line. Since you said Q_z is 5x5 transition matrix
VBBV on 25 Jul 2021
%if true
exp2=zeros(100,5,5)
for iz=1:5
for iznext=1:5 exp2(:,iz,iznext)=exp2(:,iz,iznext)+v_e(:,iznext)*Q_z(iz,iznext);
end
end
You can try this by making v_e as 100x5x5

Torsten on 21 Jul 2021
vec = ones(1,5)*Q_z.' ;
exp2(1:100,:) = vec ;
Mert Demir on 21 Jul 2021
Thanks but i need to use v_e in the form. v_e will not be ones(100,5) later on. it is just an initial guess. I will keep iterate that.

### Community Treasure Hunt

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

Start Hunting!

Translated by