The following code subtracts tolerance after it chooses a random number from each row. I have made a slight mistake here. I don’t want to subtract tolerance of the diagonal elements from the transition. how do i fix that?
any help is appreciated.
clear all;
close all;
clc;
tolerance= 0.01;
Transition = [0.06 0.47 0 0.47 0 0 0;
0.47 0.06 0.47 0 0 0 0;
0 0.47 0.06 0.47 0 0 0;
0.47 0 0.47 0.037 0.023 0 0;
0 0 0 0.023 0.037 0.47 0.47;
0 0 0 0 0.47 0.06 0.47;
0 0 0 0 0.47 0.47 0.06];
len=length(Transition);
Dij=Transition;
% Assigned status of all the sites at given time k
S_k= [0 1 1 1 1 0 0];
for i=1:7
while(1)
sel=randi(7);
if(Dij(i,sel)~=0)
show(i)=sel;
break;
end
end
Dij(i,sel)=Dij(i,sel)-tolerance;
end
I want to carry this loop till one of the non diagonal elements is zero.
If I have understood correctly then you only need to change this line
To include the condition that it is also not on the diagonal
unless you still want to call show() on the diagonals and just not do the subtration in which case just move that logic lower down i.e.
but also just one more comment.
while(1) break;isn’t a great construct. You could just havewhile(Dij(i, sel)~= 0)andDij(i, sel) = 0just above and avoid the need to call break at all.Lastly my understanding is you have opened yourself up to an infinite loop if all columns are 0. Maybe this can never happen but just something to think about.