Count number of specific values in matrix
조회 수: 1,858 (최근 30일)
이전 댓글 표시
I have a large matrix, m, and am trying to count the number of a specific value (i.e. How many indexes are of the value 4?)
I tried using
val = sum(m == 4);
but I end up with val being a matrix/vector of numbers. I assume these numbers are from each column and should be added together for the total, so I tried another
num = sum(val == 4);
but then I just end up with another vector/matrix.
How can I do it?
댓글 수: 2
Walter Roberson
2023년 10월 18일
num = sum(val == 0, 'all'); %r2018b or later
num = sum(val(:) == 0); %any version
num = nnz(~val); %any version
채택된 답변
Walter Roberson
2012년 5월 2일
sum(m(:) == 4)
댓글 수: 3
MathWorks Support Team
2020년 9월 2일
An alternative syntax available in R2018b or later is sum(m==4,'all'). But for this simple problem colonizing the input with m(:) is likely to be faster.
Andrew Frane
2024년 10월 22일
Using the colon probably won't be faster to execute. If you use m(:) then Matlab has to do the additional step of creating a new array m(:) before doing the logical evaluation. In my tests, using 'all' is consistently faster than using the colon to vectorize the array.
추가 답변 (6개)
Kye Taylor
2012년 5월 2일
Try this:
numberOfNonZeros = nnz(m==4);
Using nnz is more efficient than converting logicals to numeric, which is required to apply sum()
댓글 수: 2
Walter Roberson
2019년 8월 22일
편집: Walter Roberson
2019년 8월 22일
In the test I just did, the timings of sum() vs nnz() could not consistently tell the two cases apart. nnz() might possibly have been slightly faster, but the range of timings showed so much overlap that no real conclusion could be reached. It would make sense that nnz() could be faster, but I can't prove it at the moment. sum() on a large enough array could be dispatched to LAPACK after all.
Andrew Frane
2024년 10월 22일
I did a million iterations after defining m as a 1x1000000 vector, and nnz(m==4) beat sum(m==4) every time.
I did another million iterations after defining m as a 1000x1000 matrix, and nnz(m(:)==4) beat both sum(m(:)==4) and sum(m==4, 'all') every time.
So nnz does appear to be more efficient than sum.
Sean de Wolski
2012년 5월 2일
This could be done easily with histc() and unique() to get the number of each value:
uv = unique(x);
n = histc(x,uv);
Or with unique() and accumarray():
[uv,~,idx] = unique(x);
n = accumarray(idx(:),1)
댓글 수: 2
Royi Avital
2022년 10월 10일
Pay attention that histcount() won't have the same result as histc() above for this case (Difference at the end).
Walter Roberson
2022년 10월 11일
Royi is correct.
At the time the question was asked, histcounts did not exist.
The newer histcounts is recommended instead of histc()
In the where you pass in the bin edges, then histc() counts values that exactly match the upper limit separately, but histcounts counts them together with the previous bin.
dipanka tanu sarmah
2017년 11월 11일
along with this if you want to count the number of NaN ,(if there any) use nnz(isnan(m))
댓글 수: 0
vimal kumar chawda
2020년 5월 18일
But if we want ot do for NaN and any numeric value in large matrix then ?
ans1=sum(a==5) so at this my value is numerical (which is not same all time) and other is NaN which is common. But i need to count only numerical value at particular value of x.,x2,x3...............x7000 which is on y axis.
-How many times y appear on the at particular value of x?
댓글 수: 1
Walter Roberson
2020년 5월 18일
nnz(isnan(a)) %count nan
For a count of each value, see https://www.mathworks.com/matlabcentral/answers/37196-count-number-of-specific-values-in-matrix#answer_46452
Patrick Benz
2021년 4월 2일
How can I count the values in the second column of an array depending on the values in the column?
I've got an array that looks something like that:
400 0
396 0
392 1
400 0
396 1
400 1
and I want to know how often there is a "1" or a "0" next to a "400" or next to the other values.
I tried it with this way https://de.mathworks.com/matlabcentral/answers/37196-count-number-of-specific-values-in-matrix#answer_46452
but this only gives me the total numbers of "1" and "0" and how often there is a 392 in the first column.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!