MATLAB: Finding a value and call the cell number or counting value on the same length of vectors.

find the row and column number and counting

I'd like to call the row number when B(i,1)==1;
For i=1:n;
if B(i,1)==1;
(strfind is only for 1 row.) how to call the row and column number ?
or
for i=1:n;
if B(i,1)=1; % when B(i,1)==1, how to count 1 value and add 1
like
1 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1
1 0 0 0 2 0 0 3 0 0 0 0 4 0 0 0 0 0 0 5 0 0 0 0 0 6 0 0 0 0 7 ..
Thank you so much.

Best Answer

Using vectorized code is faster and neater than using a loop:
>> X = [1 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1];
>> Y = cumsum(X);
>> Y(X==0) = 0
Y =
1 0 0 0 2 0 0 3 0 0 0 0 4 0 0 0 0 0 0 5 0 0 0 0 0 6 0 0 0 0 7