Frequency of data occurrence using histc

I have a matrix called data which is 40*40*50 and I want to find all the distinct numbers in the matrix and count their occurrences. This code below seems to work when data is 40*40*39 or smaller, but as soon as I try to run the code on the entire data matrix it starts giving 0.001 as the frequency for all of the unique occurrences
% code
out = [unique(data),histc(data(:),a)]
plot(a, histc(data(:),a));
I know that using histc is not recommended, but it was the easiest way I could find to do this.
Thanks for any and all help.

답변 (2개)

Guillaume
Guillaume 2018년 10월 10일

0 개 추천

I presume that your numbers are integer (otherwise, you're playing dangerous games using unique with floating point values), in which case:
out = histcounts(data, 'BinMethod', 'integers');

댓글 수: 2

Edward Jahoda
Edward Jahoda 2018년 10월 10일
Thanks for the response. I am using floating point values (with 6 values after the decimal point), and consequently wondering if you have some suggestion as to how to deal with the floating points or some information on why its dangerous.
"some information on why its dangerous"
See the output of
unique([0.3, 0.1+0.1+0.1])
If you want 6 digits of precision, I would do this:
out = histcounts(floor(data * 1e6), 'BinMethod', 'integers');
to move back to integer bins.

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

Bruno Luong
Bruno Luong 2018년 10월 10일

0 개 추천

You might take a look at uniquetol()

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2018년 10월 10일

댓글:

2019년 10월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by