MATLAB: How to derive the length and area under the curve of parts in the dataset

numerical integrationsignal processingvector

I have a column
A=[0,0,1,2,3,0,0,0,1,2,4,6,0,0,1,2,3,0]
and would like to return one column with the length of each consecutive set of numbers that differs from 0. In another column I would like to return the cumulative sum of each consecutive set of numbers that differs from 0.
For A this would result in [3,4,3] and [6,13,6]
Thank you

Best Answer

mask = A(:).' ~= 0;
starts = strfind([0 mask], [0 1]);
stops = strfind([mask 0], [1 0]);
first_column = stops - starts + 1;
sums = cumsum([0 A(:).']);
second_column = sums(stops+1) - sums(starts);
output = [first_column(:), second_column(:)];