MATLAB: How can i replace a number with its no of ocurrences

replacing a number with no. of occurences

i have a matrix=a=[1 0 1 0 1;0 1 1 1 1;1 1 0 1 1;1 1 1 1 1] i want to replace ones by their no of occurences in each row like a=[1 0 2 0 3;0 1 2 3 4;1 2 0 3 4;1 2 3 4 5]

Best Answer

a=[1 0 1 0 1;0 1 1 1 1;1 1 0 1 1;1 1 1 1 1];
cumsum(a,2).*a
ans =
1 0 2 0 3
0 1 2 3 4
1 2 0 3 4
1 2 3 4 5