finding the frequency of a series of numbers in an array

조회 수: 28 (최근 30일)
Joe
Joe 2015년 6월 3일
답변: Manoel Rodrigues Trigueiro 2019년 3월 30일
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 10x2 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!

채택된 답변

Guillaume
Guillaume 2015년 6월 4일
편집: Guillaume 2015년 6월 4일
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
  댓글 수: 1
Joe
Joe 2015년 6월 4일
I've combined the answeres into one use with a threshold
data=[1,1,1,2,2,2,3,6,3,3,10,3,10];
threshold=1;
transpos = find(diff(data));
transpos1=[transpos, transpos(end)+1];
transvals = data(transpos1);
transvals(diff([0 transpos1 numel(data)+1])<threshold)=[0];
for k=1:1:10
frequency(k)=length(find(transvals==k));
end
Thanks for all the help!

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Jan
Jan 2015년 6월 4일
[value, len] = RunLength(data);
bin = min(value):max(value);
h = histc(value(len > 2), bin);
result = [bin.', h.']

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 3일
편집: Azzi Abdelmalek 2015년 6월 3일
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];
ii=unique(data);
for k=1:numel(ii)
a=data==ii(k);
b=[0 a 0];
out(k)=sum(strfind(b,[1 0])-strfind(b,[0 1])>=2);
end
out
  댓글 수: 2
Joe
Joe 2015년 6월 4일
If there are a fixed number of places in this array (for example k=1:1:10) and the data array is missing the number 6
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]
is there a way to output a zero in its place to make it a 10x1 array?
Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 4일
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];
ii=min(data):max(data);
for k=1:numel(ii)
a=data==ii(k);
b=[0 a 0];
out(k)=sum(strfind(b,[1 0])-strfind(b,[0 1])>=2);
end
out

댓글을 달려면 로그인하십시오.


Suvidha Tripathi
Suvidha Tripathi 2018년 10월 24일
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]
unique_data=unique(data);
disp("series_no "+"frequency")
for i=1:length(unique_data)
[~,indices]=find(data==unique_data(i));
freq(i)=length(indices);
disp(unique_data(i)+" "+freq(i))
end

Manoel Rodrigues Trigueiro
Manoel Rodrigues Trigueiro 2019년 3월 30일
% Frequency order function %
function f = freq(X)
a = unique(X); len_X = length(a);
for i=1:len_X
f(i) = mode(X);
ind = find(mode(X)==X);
for j=1:length(ind)
X(ind(j)) = NaN;
end
end
end

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by