simplying of the statements
Afficher commentaires plus anciens
I have attached two parts(part 1 and part 2) from my script and there are few parts like this and I want to simplify these hard coded statements. For easyness, I have attached two different statements only in here which means I just want understand my weakness once somebody guided me. All these i,j,k values are in 1:10 range. just expecting two different simplified statements for part(1) and part(2).
if ((j==1)&&(k==1)||(j==2)&&(k==2)||(j==3)&&(k==3)||(j==4)&&(k==4)||(j==5)&&(k==5)||(j==6)&&(k==6)||(j==7)&&(k==7)||(j==8)&&(k==8)||(j==9)&&(k==9)||(j==10)&&(k==10))&&((i==1)||(i==10));
B(i,j,k)=1;
else B(i,j,k)=0;%----------part (1)
if ((i==10)&&(j==1)||(i==9)&&(j==2)||(i==8)&&(j==3)||(i==7)&&(j==4)||(i==6)&&(j==5)||(i==5)&&(j==6)||(i==4)&&(j==7)||(i==3)&&(j==8)||(i==2)&&(j==9)||(i==1)&&(j==10))&&((k==1)||(k==10));
B(i,j,k)=1;
else B(i, j,k)=0;%----------part(2)
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 30 Juil 2020
B(i,j,k) = ismember(i,[1 10]) && ismember(j, 1:10) && j == k; %part 1
B(i,j,k) = ismember(k,[1 10]) && ismember(j, 1:10) && i+j == 11; %part 2
Depending on the permitted range of j values, there would be some cases where these could be simplified to
B(i,j,k) = ismember(i,[1 10]) && j == k; %part 1
B(i,j,k) = ismember(k,[1 10]) && i+j == 11; %part 2
3 commentaires
Hege
le 30 Juil 2020
Walter Roberson
le 30 Juil 2020
if ((j==1)&&(k==1)||(j==2)&&(k==2)||(j==3)&&(k==3)||(j==4)&&(k==4)||(j==5)&&(k==5)||(j==6)&&(k==6)||(j==7)&&(k==7)||(j==8)&&(k==8)||(j==9)&&(k==9)||(j==10)&&(k==10))&&((i==1)||(i==10));
(j==1)&&(k==1) -- okay that is starting conditions that do not necessarily mean much in themselves but we will keep them in mind
(j==2)&&(k==2) -- oh look, j is the same as k, and that was true for (j==1)&&(k==1) -- maybe that is the pattern ?
(j==3)&&(k==3) -- yup, still j == k. And that pattern carries through to (j==10)&&(k==10))&&((i==1)||(i==10))
So the part in () up to but excluding the &&((i==1)||(i==10)) has the rule that j and k must be equal and that they must be in the range 1 to 10. Because they must be equal, you can skip testing that they are both in the range 1 through 10 if you have already tested that they are equal.
The ((i==1)||(i==10)) part can be written multiple ways. Since I already used ismember(j, 1:10) to enforce that j and k are 1, 2, 3, ... 10 (and not, for example, -2 or 7.5), then it makes sense to use the same style and test ismember(i, [1 10])
if ((i==10)&&(j==1)||(i==9)&&(j==2)||(i==8)&&(j==3)||(i==7)&&(j==4)||(i==6)&&(j==5)||(i==5)&&(j==6)||(i==4)&&(j==7)||(i==3)&&(j==8)||(i==2)&&(j==9)||(i==1)&&(j==10))&&((k==1)||(k==10));
That starts with (i==10)&&(j==1) and after seeing the previous part, our thought will be along the lines of whether j will increase at the same rate that i increases, or whether one of them will decrease when the other one increases. And (i==9)&&(j==2) immediately focuses us on i decreasing at the same rate that j increases. We can see quickly that the same pattern holds up to (i==1)&&(j==10) . So the sum is constant and we can use that as the test.
Hege
le 30 Juil 2020
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!