MATLAB: How to count the number of other elements between two specific elements in a matrix

countingmatrixrow

Hi Everyone,
I am working on a really simple project. I have a matrix A:
A =[0 0 0 1 1 0 0 0;
0 0 1 0 0 1 0 0;
0 1 0 0 0 0 1 0;
0 1 0 0 0 0 1 0;
0 0 1 0 0 1 0 0;
0 0 0 1 1 0 0 0];
The question is, how can I count how many 0 elements do I have between two 1 elements?
For example, in the second row, I have 2 zero elements between the two 1 elements. In the third row, I have 4 zero elements between the two 1 elements.
Many thanks in advance.

Best Answer

Assuming that there are only two ones per row:
>> sum(cumsum(A,2)==1,2)-1
ans =
0
2
4
4
2
0