MATLAB: How do i use different parts of a variable for the same code

iterationmatrixmatrix manipulation

I have this code in which i have to calculate the Gn variable shown below for two different iterations. In each iteration the value of D(a matrix) changes. My question is how do i make it that for the first iteration the variables dn,dtx,dty,dtz and d0n use only the four numbers of m0 as it is now but for the second iteration it uses the last four numbers of m0.
outd0 = cell(2,1);
outGt = cell(1,2);
for iter=1:2
m0=[0; 0; 0; 0; 0 ;0 ;0 ;0];
sG=stationGroups{iter};
n=length(sG);
rowVector=ones(1,n);
dt0=rowVector;
D=sG';
dn=sqrt((D(1,:)-m0(2)).^2+(D(2,:)-m0(3)).^2+(D(3,:)-m0(4)).^2);
dtx = -(D(1,:)-m0(2))./(v.*dn);
dty = -(D(2,:)-m0(3))./(v.*dn);
dtz = -(D(3,:)-m0(4))./(v.*dn);
Gn=[dt0; dtx; dty; dtz];
Gt=Gn';
outGt{iter} = Gt;
Gt1=cell2mat(outGt(1,1));
Gt2=cell2mat(outGt(1,2));
G=blkdiag(Gt1,Gt2);
d0n=(m0(1)+(vecnorm(repmat(m0(2:4),1,n)-D(:,1:n)))./v);
d0t=d0n';
outd0{iter} = d0t;
d01=cell2mat(outd0(1,1));
d02=cell2mat(outd0(2,1));
d0=[d01;d02];
end

Best Answer

Use an if statement
if iter == 1
dn=sqrt((D(1,:)-m0(2)).^2+(D(2,:)-m0(3)).^2+(D(3,:)-m0(4)).^2);
dtx = -(D(1,:)-m0(2))./(v.*dn);
dty = -(D(2,:)-m0(3))./(v.*dn);
dtz = -(D(3,:)-m0(4))./(v.*dn);
else
dn=sqrt((D(1,:)-m0(6)).^2+(D(2,:)-m0(7)).^2+(D(3,:)-m0(8)).^2);
dtx = -(D(1,:)-m0(6))./(v.*dn);
dty = -(D(2,:)-m0(7))./(v.*dn);
dtz = -(D(3,:)-m0(8))./(v.*dn);
end
However note that you set m0 to all zeros on every iteration so I don't see why you're even wanting to use it.