Thanks so much for the help! The code ran but the results seems a little strange... essentially what I am trying to do is to convert my matrix (in this example below matrix5, its a 22*22 matrix) to its closest stochastic matrix with the below logic: 1) go through the matrix row by row starting with row 1 2) if any of the element in row 1 is negative, then replace the negative number with 0 and if the rest in the row are positive, then normalize all other elements with ind=loc(a>0); b=a[ind]; k=nrow(b)#ncol(b); b=b-1/k#(sum(b)-1); a[ind]=b; keep on looping till all elements are >=0 3) once row 1 satisfies the above condition, go to row 2, and keep on going till row 22 4) output to a below is my updated code:
proc iml;
use OLD;
read all var _ALL_ into matrix5[colname=varNames];
close OLD;
a=matrix5;
do ii=1 to nrow(matrix5);
call symputx("iii",ii);
a=matrix5[num(symget("iii")), ];
do while (any(a<0));
a[loc(a<0)]=0;
ind=loc(a>0);
b=a[ind]; k=nrow(b)#ncol(b);
b=b-1/k#(sum(b)-1);
a[ind]=b;
end;
rA[num(symget("iii")), ] = a;
print rA[L="new4" format=7.4];
end;
quit;
any help would be greatly appreciated!
... View more