i want to generalize and reduce following program into one loop

조회 수: 3 (최근 30일)
Mudasir Ahmed
Mudasir Ahmed 2014년 10월 7일
댓글: Mudasir Ahmed 2014년 10월 7일
chx1=genx1 chx2=genx2 chx3=genx3 chx4=genx4 chx5=genx5
chy1=geny1 chy2=geny2 chy3=geny3 chy4=geny4 chy5=geny5
if ((gudx1>0) & (gudx2>0)) if (chx1==genx1) chx1= gudx2; end end
if ((gudx1>0) & (gudx3>0)) if (chx1==genx1) chx1= gudx3; end end
if ((gudx1>0) & (gudx4>0)) if (chx1==genx1) chx1= gudx4; end end
if ((gudx1>0) & (gudx5>0)) if (chx1==genx1) chx1= gudx5; end end
if ((gudx2>0) & (gudx3>0)) if (chx2==genx2) chx2= gudx3; end end
if ((gudx2>0) & (gudx4>0)) if (chx2==genx2) chx2= gudx4; end end
if ((gudx2>0) & (gudx5>0)) if (chx2==genx2) chx2= gudx5; end end
if ((gudx3>0) & (gudx4>0)) if (chx3==genx3) chx3=gudx4; end end
if ((gudx3>0) & (gudx5>0)) if (chx3==genx3) chx3=gudx5; end end
if ((gudx4>0) & (gudx5>0)) if (chx4==genx4) chx4= gudx5; end end
if ((gudx5>0) & (gudx1>0)) if (chx5==genx5) chx5= gudx1; end end
if ((gudx5>0) & (gudx2>0)) if (chx5==genx5) chx5= gudx2; end end
if ((gudx5>0) & (gudx3>0)) if (chx5==genx5) chx5= gudx3; end end
if ((gudx5>0) & (gudx4>0)) if (chx5==genx5) chx5= gudx4; end end
%y
if ((gudy1>0) & (gudy2>0)) if (chy1==geny1) chy1= gudy2; end end
if ((gudy1>0) & (gudy3>0)) if (chy1==geny1) chy1= gudy3; end end
if ((gudy1>0) & (gudy4>0)) if (chy1==geny1) chy1= gudy4; end end
if ((gudy1>0) & (gudy5>0)) if (chy1==geny1) chy1= gudy5; end end
if ((gudy2>0) & (gudy3>0)) if (chy2==geny2) chy2= gudy3; end end
if ((gudy2>0) & (gudy4>0)) if (chy2==geny2) chy2= gudy4; end end
if ((gudy2>0) & (gudy5>0)) if (chy2==geny2) chy2= gudy5; end end
if ((gudy3>0) & (gudy4>0)) if (chy3==geny3) chy3=gudy4; end end
if ((gudy3>0) & (gudy5>0)) if (chy3==geny3) chy3=gudy5; end end
if ((gudy4>0) & (gudy5>0)) if (chy4==geny4) chy4= gudy5; end end
if ((gudy5>0) & (gudy1>0)) if (chy5==geny5) chy5= gudy1; end end
if ((gudy5>0) & (gudy2>0)) if (chy5==geny5) chy5= gudy2; end end
if ((gudy5>0) & (gudy3>0)) if (chy5==geny5) chy5= gudy3; end end
if ((gudy5>0) & (gudy4>0)) if (chy5==geny5) chy5= gudy4; end end
ch1=[chx1 chy1] ch2=[chx2 chy2] ch3=[chx3 chy3] ch4=[chx4 chy4] ch5=[chx5 chy5]
i want to generalize above program and size of chx[1...5] and chy[1...5] may increase or decrease depend on user

채택된 답변

José-Luis
José-Luis 2014년 10월 7일
And this is a prime example of why you should store your variables in a single matrix instead of with "numbered" names.
So the answer would be:
Place all gudxn and gudyn in a single array and loop through that array instead.
gudx(1) = gudx1;
gudx(2) = gudx2;
Ad nauseam...
  댓글 수: 4
José-Luis
José-Luis 2014년 10월 7일
I'm sorry but I don't follow your logic. Are you testing for all possible permutations of gudx and gudy?
Mudasir Ahmed
Mudasir Ahmed 2014년 10월 7일
A1=B1 A2=B2 A3=B3 A4=B4 A5=B5 %save values B in A
if ((X1>0) & (X2>0)) if (A1==B1) A1=X2; end end
if ((X1>0) & (X3>0)) if (A1==B1) A1=X3; end end
actually i want to pair in the following sense 1-2 1-3 1-4 1-5 2-3 2-4 2-5 3-4 3-5 4-5 5-1 5-2 5-3 5-4
pair with itself and decreasing one is not allowed except the final variable, who can pair with all variable below him except himself.
and 2nd main point is changing of any variable A1,A2,A3,A4,A5 is allowed only one time, means we cannot change a value of any variable twice or thrice.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dimensionality Reduction and Feature Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by