MATLAB: I want count iteration in a binary matrix as follows.

count binary iteration

I want count iteration in a binary matrix as follows. I=[1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1] my answer should like [4 8 5 3 5]

Best Answer

Try this:
I=[1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1];
dI = diff(find([1 diff((I) ~= 0) length(I)]));
dI =
4 8 5 3 5