simplying of the statements

1 次查看(过去 30 天)
评论: Hege ,2020-7-30
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));
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));
else B(i, j,k)=0;%----------part(2)


Sriram Tadavarty
Sriram Tadavarty 2020-7-30
Hi Yasasween,
Two things to make a simplied equation:
  1. Assigning B with zeros, as all the dimensions are known at the first place. Implies, one can get away with else condition (i.e. B = zeros(10,10,10)
  2. Now, placing the if conditions for the value of 1
On the Simplication part, lets start with the first condition placed
% First condition
% As can be seen in the above condition, the first part has j and k are equal, and then it is valid for i equals 1 or 10
% So, this can be the update or simplied statement for it
((j == k)) && (any(i == [1 10]))
% Second condition
% As can be seen in the above condition, other than the last condiiton, all other conditions lead to a value of 11 when added
((i+j == 11)) || (any(k == [1 10]))
% In a similar fashion, based on the pattern of the conditions, the simplications can be made
Hope this helps.
  1 个评论
Hege 2020-7-30
Hi Sriram, Thank you so much for th explanation. This is working for my script.Thank you.


更多回答(1 个)

Walter Roberson
Walter Roberson 2020-7-30
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 个评论
Hege 2020-7-30
Hi Walter. Thank you so much for the explanation. This works very well. Thank you.





Community Treasure Hunt

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

Start Hunting!

Translated by