how to compute blocks of ones in a row

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 Comments

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?
imola
imola on 18 Mar 2015
Edited: imola on 18 Mar 2015
Dear Sean,
I regards every block of ones as 1, then account the length of the block, see my code, it gives me the index of the blocks if I account them, I got 3 blocks for first row and 2 for second row, but how to find the length for every block.
thanks for answering me.
regards,
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?
And by the way, don't call your variable sum

Sign in to comment.

 Accepted Answer

A simple way to get the lengths and number of blocks:
P = [1 0 1 1 0 0 1 1 1];
transitions = diff([0 P 0]);
%transitions is 1 when going from 0 to 1 and -1 when going from 1 to 0.
%because 0 is added before and after, the first transition is always 0 to 1 and last always 1 to 0
%so there's always the same numbers of 1 and -1 in the diff
numblocks = sum(transitions == 1) %or sum(transitions == -1)
blocklengths = find(transitions == -1) - find(transitions == 1)
To operate on a whole matrix at once is a bit trickier:
P = [1 0 1 1 0 0 1 1 1
0 0 1 0 1 1 0 0 0];
transitions = diff([zeros(size(P, 1), 1) P zeros(size(P, 1), 1)], [], 2);
[starts, rows] = find(transitions' == 1);
[ends, ~] = find(transitions' == -1);
blocklengths = ends - starts;
sumlengthsbyrow = accumarray(rows, blocklengths, [size(P, 1) 1])
numblocksbyrow = accumarray(rows, 1, [size(P, 1) 1])
totalsumbyrow = sumlengthsbyrow + numblocksbyrow
If you don't want to include the blocks of length 1 in sumlengthsbyrow:
sumlengthsbyrow = accumarray(rows(blocklengths > 1), blocklengths(blocklengths > 1), [size(P, 1) 1])

3 Comments

Dear Guillaume,
Thank you for the useful answer. that's great. But if I don't want to account the single 1 length, and I need to set it to zero such that the row [1 0 0 1 1] will equal 4 not 5 and [1 0 1 1 0 0 1 1 1] will equal 8
I mean the result should be
sumlengthsbyrow =[5 ; 2]
numblocksbyrow =[3;2]
totalsumbyrow =[8;4]
thanks
As I wrote at the end of my answer, if you don't want to include the lengths of one, it's:
sumlengthsbyrow = accumarray(rows(blocklengths > 1), blocklengths(blocklengths > 1), [size(P, 1) 1])
numblocksbyrow = accumarray(rows, 1, [size(P, 1) 1])
totalsumbyrow = sumlengthsbyrow + numblocksbyrow
Dear Guillaume,
Thanks a lot. I really appreciate your help.
regards

Sign in to comment.

More Answers (0)

Asked:

on 16 Mar 2015

Commented:

on 18 Mar 2015

Community Treasure Hunt

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

Start Hunting!