How to determine the most common (most occurring) number in column of a large data of more than 100,000 length of data
조회 수: 6 (최근 30일)
이전 댓글 표시
How to determine the most common (most occurring) number in column of a large data of more than 100,000 length of data?
댓글 수: 0
답변 (4개)
Star Strider
2018년 5월 2일
If you want to count a range of values, rather than exact values, one option is to use the histogram (link) function (or the hist (link) function). You can use the number of bins you want with either function. If you want to define the bins themselves, you will need to define the edges of the bins in histogram and the centres of the bins in hist.
댓글 수: 0
Ameer Hamza
2018년 5월 2일
mostOccuringVal = mode(A);
댓글 수: 2
Jan
2018년 5월 3일
@Gali: Please mention the important details in the question already, not only in a comment after somebody has posted an answer.
John D'Errico
2018년 5월 2일
편집: John D'Errico
2018년 5월 2일
There are many things you can do. But none will likely be perfectly satisfactory. For example, you could use uniquetol to do the "counting".
[Vuniq,I,J] = uniquetol(V,0.01);
counts = accumarray(J,1,[100,1],@sum);
[cmax,ind] = max(counts)
cmax =
1094
ind =
37
Vuniq(ind)
ans =
0.36033
So the most frequent value, with a bin of 0.01, and a count of 1106 was 0.36033. The bins that were implicitly created by uniquetol have a width of approximately 0.01. This is essentially the same solution that would arise has a histogram tool been used, as long as the bin boundaries were the same.
That is, the first 10 such unique results obtained from uniquetol are:
Vuniq(1:10)'
ans =
6.2251e-06 0.010016 0.020018 0.030019 0.040023 0.050026 0.060033 0.070049 0.080051 0.090053
diff(ans)
ans =
0.01001 0.010002 0.010001 0.010004 0.010003 0.010007 0.010016 0.010002 0.010002
But was 0.36033 the truly most common? Suppose that the most frequent count happened to cross two such bins?
As I said, there is no perfect solution, at least probably not if you want it to be fast. Are you looking for ANY interval of width 0.01 that contains the most number of elements? If so, this will get more difficult. Still doable, but possibly a bit slower, with more effort. You can see that I chose a vector V that was intentionally going to be very difficult in this respect.
Vs = sort(V);
[~,~,upperbin] = histcounts(Vs + 0.01,Vs);
[Vmaxcount,Vsind] = max(upperbin' - (0:100000 - 1))
Vmaxcount =
1106
Vsind =
35963
So it looks like the interval of width 0.01 with the MAXIMUM number of elements in the vector V seems to be [0.36073,0.36073 + 0.01].
Vs(Vsind)
ans =
0.36073
As a test:
sum((Vs >= Vs(Vsind)) & (Vs < Vs(Vsind) + 0.01))
ans =
1106
So arguably, the true moving mode, with an interval width of 0.01 is:
Vs(Vsind) + 0.01/2
ans =
0.36573
Surprisingly the best interval of width 0.01 was actually one that overlapped with the one that uniquetol found. But there is no reason this must happen. Had I chosen a different random set of data, that could easily change.
Anyway, because I was able to use efficient tools for this, it was even pretty fast.
댓글 수: 0
Siyu Guo
2018년 5월 3일
Suppose v is your data vector.
u = unique(v);
h = hist(v,u);
[~,i] = max(h);
value_with_most_occurrences = u(i);
댓글 수: 2
John D'Errico
2018년 5월 3일
Which does exactly the same thing as mode(v), but takes 4 lines, instead of 1.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!