MATLAB: Count the “areas” of zeros and the “areas” of ones in a vector

matrixmatrix manipulationvector

I have a vector:
a=[0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 0 1 ];
in which I would like to count the "areas" of zeros and the "areas" of ones. In the example above I have
4 areas of zeros and 4 areas of ones. Thank you!

Best Answer

a=[0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 0 1 ];
lc = [true;diff(a(:))~=0];
x = a(lc);
zerosareas = sum(~x);
onesareas = sum(x);