Here is a profile of the call n = 2*10^3; M = DStochMat02(n,ones(n)./n);
n = 2*10^3; M = DStochMat02(n,ones(n)./n);
time calls line 1 function M = DStochMat02(n,c)
How about changing lines 14 and 15 to the following lines:
14
15
l = ( [ pidx ; 1:n ] - 1 ) * [1;n] + 1; % convert pairs (pidx,1:n) to linear indices M(l) = M(l) + c(k);
since P is very sparse, maybe it would be more efficient to increment only the non-zeros of P.
P