similar code for various values of k in a for loop?

2 ビュー (過去 30 日間)
JAGADEESH JAGA
JAGADEESH JAGA 2017 年 2 月 11 日
回答済み: Walter Roberson 2017 年 2 月 11 日
m=2
T=[1 0 0; 0 (1/sqrt(2)) (1/sqrt(2))];
V1=0.956
V2=2.4
for k=1:3
if k==1
A=[ 0 1 0];
B=[0 1 1];
D=B-A;
d1=m*A;
for X=1:1:m+1
C=d1+D*(X-1)
c=transpose(C);
Vndq=T*c;
Vnq=Vndq(1,1)
Vnd=Vndq(2,1)
d(X)=abs(V2-Vnq)+abs(V1-Vnd)
L=min(d)
end
elseif k==2
A=[1 0 0]
B=[1 0 1]
D=B-A;
d1=m*A;
for X=1:1:m+1
C=d1+D*(X-1)
c=transpose(C);
Vndq=T*c;
Vnq=Vndq(1,1)
Vnd=Vndq(2,1)
d(X)=abs(V2-Vnq)+abs(V1-Vnd)
L=min(d)
end
else k==3
A=[1 0 1]
B=[1 0 2]
D=B-A;
d1=m*A;
for X=1:1:m+1
C=d1+D*(X-1)
c=transpose(C);
Vndq=T*c;
Vnq=Vndq(1,1)
Vnd=Vndq(2,1)
d(X)=abs(V2-Vnq)+abs(V1-Vnd)
L=min(d)
end
end
end
In the above given A ,B matrices changes for every value of k and the remaining code is same for various values of k. how to use the same code for various values of k by writing the code only once which is similar for all the values of k

採用された回答

Walter Roberson
Walter Roberson 2017 年 2 月 11 日
I suggest you create a function to do the calculation, passing in A and B.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by