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

조회 수: 1 (최근 30일)
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

채택된 답변

Image Analyst
Image Analyst 2018년 5월 9일
편집: Image Analyst 2018년 5월 9일
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
Feliciano Döring
Feliciano Döring 2018년 5월 9일
Yeah i put it wrong the m0 is supposed to be outside the loop, thanks!

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

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2018년 5월 9일
m0=[1 2 3 4; 10 20 30 40];
for iter=1:2
m0(iter,1)
m0(iter,2)
end
  댓글 수: 1
Feliciano Döring
Feliciano Döring 2018년 5월 9일
But then m0 isn't a column vector, i need it to be a column vector

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by