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

1 view (last 30 days)
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

Stephen23
Stephen23 on 17 Nov 2015
Edited: Stephen23 on 17 Nov 2015
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

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!