how do i use different parts of a variable for the same code?

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

 Réponse acceptée

Image Analyst
Image Analyst le 9 Mai 2018
Modifié(e) : Image Analyst le 9 Mai 2018
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.

1 commentaire

Yeah i put it wrong the m0 is supposed to be outside the loop, thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

m0=[1 2 3 4; 10 20 30 40];
for iter=1:2
m0(iter,1)
m0(iter,2)
end

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by