MATLAB: How can I create cumulative matrix.

cumulative matrix

Hello a question please.
I have matrix P with size (452*500). I want to select random selection with roulette wheel selection. I write something:
r=rand;
c=reshape(p,1,[]);
c1=cumsum(c);
j=find(r<=c1,1,'first');
[j1 j2]=ind2sub(size(p),j);
end
but I want to remove part " c=reshape(p,1,[]);" and create cumulative matrix of p directly without reshaping it to matrix c. How can I do it?
Thank you.

Best Answer

Something like this? :
r = rand;
jj = find(r<=cumsum(p(:)),1,'first');
[j1,j2] = ind2sub(size(p),jj);
Note I also renamed your variable j, which is also one of names of the inbuilt imaginary unit .