MATLAB: Counting data points

countsum

I have a data set of 1's and 0's like this 000001111100000111110000011111000001111100000 this data set is called "c"
I need the program to output a count of sections so to speak. In this example they are all even and short but in my data set they are extremely long and of varying lengths. If I could get an output like 5,5,5,5,5,5,5 counting each section it would save me tons of time.

Best Answer

If you have the "c" as an integer or logical array, and if you have the Image Processing Toolbox, you can do it in one line with regionprops(). Just simply say "measurements = regionprops(~c, 'Area')" Here's a full demo:
% Create sample data.
c = [0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0]
% Measure the zero sections - KEY LINE OF CODE RIGHT HERE!
measurements = regionprops(~c, 'Area')
% Display results.
fprintf('The number of zero sections is %d.\n', length(measurements));
fprintf('The sizes of zero sections are:\n');
allAreas = [measurements.Area]