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

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.

 Accepted 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

More Answers (0)

Categories

Asked:

on 17 Nov 2015

Edited:

on 17 Nov 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!