MATLAB: Counting subrows in each row of a matrix

matricesmatrixmatrix arraymatrix manipulation

Hi all, I need an algorithm which counts how many adjacent and non-overlapping (1,1) I have in each row of a matrix A mx(n*2) without using loops. E.g.
A=[1 1 1 0 1 1 0 0 0 1; 1 0 1 1 1 1 0 0 1 1] %m=2, n=5
Then I want
B=[2;3] %mx1
In fact A=[(1 1) (1 0) (1 1) (0 0) (0 1); (1 0) (1 1) (1 1) (0 0) (1 1)]. Then, according to this separation, I have 2 (1 1) in the first row and 3 (1 1) in the second row.

Best Answer

Assuming that n is defined previously as
>> n = 5 ;
(for the case of your example) here is a one liner
>> B = sum( reshape( all( reshape( A.', 2, [] )), n, [] )).'
B =
2
3