MATLAB: Finding the frequency of a series of numbers in an array

frequencyseries

given the data set
data=[1,1,1,2,2,3,3,3,4,4,3,3,5,5,5,6,7,8,9,9,9,1,1,1,1,10,10,1,1,1,2,2,2,2,1];
I want to find the number of occurrences (frequency) of each series of numbers in the array. For example, I know there are eleven places that 1 occurs.
k=1
location=find(data==k)
location = 1 2 3 22 23 24 25 28 29 30 35
But I want the frequency (number of series that occur in the data set for a given value in the array. Also, I want to exclude series of data in the frequency count if they fall below a threshold value (arbitrarily a value of 2 in this case). The output for the one value should be freq=3
I want the output to be a 10×2 array with the frequency of each number value 1 through 10 to be sorted in order
seriesfreq=
1 3
2 2
3 1
4 1
5 1
6 0
7 0
8 0
9 1
10 1
Please help!

Best Answer

A method with no loop:
data=[1,1,1,2,2,3,3,3,4,4,3,3,5,5,5,7,8,9,9,9,1,1,1,1,10,10,1,1,1,2,2,2,2,1];
threshold = 2;
transpos = find(diff(data)); %gives the position of the end of each series of identical number
transvals = data(transpos); %gives the corresponding value of each series
transvals(diff([0 transpos numel(data)+1]) < threshold) = []; %remove values for which the run is shorter than threshold
bins = min(data):max(data)';
[bins; histcounts(transvals, [bins Inf])]' %get the histogram of the runs