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

1 回表示 (過去 30 日間)
Feliciano Döring
Feliciano Döring 2018 年 5 月 9 日
コメント済み: Feliciano Döring 2018 年 5 月 9 日
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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by