how to compute blocks of ones in a row
Ältere Kommentare anzeigen
Dear All,
I have this matrix.
P=[1 0 1 1 0 0 1 1 1
0 0 1 0 1 1 0 0 0];
I need to accumulate the ones numbers but as blocks of ones with the length of the block, here I have in the first row 3 blocks 1 , 1 1 , 1 1 1 , so the sum is 1+1+1 for the blocks and 2 for the length 1 1 and 3 for 1 1 1 so
sum=1+1+1+2+3
in the second row
sum=1+1+2
I wrote this
ind =strfind([0, P(1,:)],[0 1])
or
ind =find (diff([0 P(1,:)])==1)
it gives me the number of blocks but not the length of every block. I need to write the formula but is rather confuse. could anybody help me please
regards,
Imola
4 Kommentare
Sean de Wolski
am 16 Mär. 2015
I don't understand how you got the 2,3. Wouldn't it be 1+1+1, and 1+2+3? Or do you not count blocks with only one 1?
Guillaume
am 18 Mär. 2015
You've not really answered Sean question.
In your example, for the first row you've got three blocks (so first part of sum is 3*1). Three block would seem to imply three lengths: [1 2 3]. Hence we would expect the complete sum to be 3*1 + 1 + 2 + 3. Why are you not accounting for the blocks of length 1?
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Signal Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!