Count number of specific values in matrix

2 045 vues (au cours des 30 derniers jours)
Corey Bullard
Corey Bullard le 2 Mai 2012
Commenté : Walter Roberson le 18 Oct 2023
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 commentaires
Dev
Dev le 18 Oct 2023
What if the value is zero?, how do I count then?
Walter Roberson
Walter Roberson le 18 Oct 2023
num = sum(val == 0, 'all'); %r2018b or later
num = sum(val(:) == 0); %any version
num = nnz(~val); %any version

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 2 Mai 2012
sum(m(:) == 4)
  2 commentaires
Romy Wolstencroft
Romy Wolstencroft le 22 Août 2019
This worked perfectly for me. Thank you
MathWorks Support Team
MathWorks Support Team le 2 Sep 2020
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.

Connectez-vous pour commenter.

Plus de réponses (6)

Kye Taylor
Kye Taylor le 2 Mai 2012
Try this:
numberOfNonZeros = nnz(m==4);
Using nnz is more efficient than converting logicals to numeric, which is required to apply sum()
  1 commentaire
Walter Roberson
Walter Roberson le 22 Août 2019
Modifié(e) : Walter Roberson le 22 Août 2019
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.

Connectez-vous pour commenter.


Sean de Wolski
Sean de Wolski le 2 Mai 2012
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 commentaires
Royi Avital
Royi Avital le 10 Oct 2022
Pay attention that histcount() won't have the same result as histc() above for this case (Difference at the end).
Walter Roberson
Walter Roberson le 11 Oct 2022
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.

Connectez-vous pour commenter.


ntsh kr
ntsh kr le 12 Oct 2017
Modifié(e) : ntsh kr le 12 Oct 2017
>> a
a =
5 5 5 5 5 5 5 6 9 96
5 3 9 5 2 7 5 6 2 1
8 3 6 9 8 7 5 1 6 9
>> ans1=sum(a==5)
ans1 =
2 1 1 2 1 1 3 0 0 0
>> b=sum(ans1)
b =
11
  1 commentaire
Manoj Payani
Manoj Payani le 16 Mai 2018
Many Thanks - It works perfect.

Connectez-vous pour commenter.


dipanka tanu sarmah
dipanka tanu sarmah le 11 Nov 2017
along with this if you want to count the number of NaN ,(if there any) use nnz(isnan(m))

vimal kumar chawda
vimal kumar chawda le 18 Mai 2020
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?

Patrick Benz
Patrick Benz le 2 Avr 2021
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.
but this only gives me the total numbers of "1" and "0" and how often there is a 392 in the first column.
  4 commentaires
Corey Bullard
Corey Bullard le 2 Avr 2021
Another year, another answer to this decade old question.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by