# Count number of specific values in matrix

4,163 views (last 30 days)
Corey Bullard on 2 May 2012
Commented: Walter Roberson on 3 Apr 2021
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?

Walter Roberson on 2 May 2012
sum(m(:) == 4)
##### 2 CommentsShowHide 1 older comment
MathWorks Support Team on 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.

Kye Taylor on 2 May 2012
Try this:
numberOfNonZeros = nnz(m==4);
Using nnz is more efficient than converting logicals to numeric, which is required to apply sum()
Walter Roberson on 22 Aug 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.

Sean de Wolski on 2 May 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)

ntsh kr on 12 Oct 2017
Edited: ntsh kr on 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
Manoj Payani on 16 May 2018
Many Thanks - It works perfect.

dipanka tanu sarmah on 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 on 18 May 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?
Walter Roberson on 18 May 2020

Patrick Benz on 2 Apr 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.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://uk.mathworks.com/matlabcentral/answers/37196-count-number-of-specific-values-in-matrix?s_tid=faqs_link", "fetch_time": 1632783330000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2021-39/segments/1631780058552.54/warc/CC-MAIN-20210927211955-20210928001955-00030.warc.gz", "warc_record_offset": 615109433, "warc_record_length": 29528, "token_count": 885, "char_count": 2866, "score": 3.5625, "int_score": 4, "crawl": "CC-MAIN-2021-39", "snapshot_type": "latest", "language": "en", "language_score": 0.9100650548934937, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00062-of-00064.parquet"}