1.75

1.8 | 8 ratings Rate this file 34 Downloads (last 30 days) File Size: 426 Bytes File ID: #3327

Count

by Richard Medlock

 

24 Apr 2003 (Updated 28 Apr 2003)

Counts the number elements in a specified vector that match a specified criteria.

| Watch this File

File Information
Description

Count(A,B)

COUNT (A,B)

Counts the number of elements in A that match the criteria specified in B.

Example:

Data = [1 2 3 4 3 2 7 6 9 1 1 2 5 9 9];

Count(Data,'==9')

ans =

     3

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (12)
28 Apr 2003 Denis Gilbert

The function contains too much computing overhead. It also includes an unnecessary EVAL statement. One will usually be better off using the built-in SUM function together with the power of logical indexing.

Example:

>> Data = [1 2 3 4 3 2 7 6 9 1 1 2 5 9 9];
>> sum(Data == 9)

ans =

     3

HTH, Denis.

28 Apr 2003 luverly loony

could also use the 'find' command coupled with 'size' to do the same thing.
>> size(find(Data==9))

28 Apr 2003 R M

Yes, it is true this is not the most efficent function.

I wrote it before I realised there was a find function - (see the date in the help comments) I didn't bother to change it.

09 Jun 2004 Fabio Wegmann

Hi I found the lack of a COUNT command quite astonishing. The problem with the solutions offered here is that they 'deliver more than needed' (i.e. position of the element). On a CPU instruction level, this requires an unneeded write to RAM, where an CPU register increment would suffice for the purpose of COUNT. I made three tests with a vector of 64800 elements:

Option 1:
if 1== 1
tic
sum(listeNZ==8)
toc
end

1603 occurences of 8
elapsed time: 0.0051

Option 2:
if 1== 1
tic;
length(find(listeNZ==8))
toc
end

1603 occurences of 8
elapsed time: 0.0038 !!!!!!!! :o (about 30% less time needed)

just for fun:
Option 3:
if 1==1
sums=0;
tic;
for k=1:64800
if listeNZ(k)==8
sums=sums+1;
end
toc
end

1603 occurences of 8
elapsed time: 0.1401 !!!!!!!! :O (about 40 times slower than option 2)

In conclusion, the times needed are too small to worry about, but a COUNT command should really be included in the standard nevertheless... I am really astonished that the length(find()) combo is faster than the sum(), though...

08 Sep 2005 Dimitri Shvorob

Duplicates built-in FIND.

18 Feb 2006 Jos van der Geest

Poor help and worse programming style. This file should be removed.

08 Jun 2006 Anil Ramachandran

dude, just use size(find(index == i),1) to find the number of times i exists in index.

18 Oct 2006 eddy smith

If last comments have not shown me how to count using sum or find your script would be very usefull.
Your script allowed me to find a good solution. Thanks to you and to the reviewers

23 Mar 2007 olcay onder

Thanks for help.t's very usefull.

18 Oct 2011 Mark

For me histc(x,unique(x)) did what I wanted. This gives me the frequency of each value that occurs in the vector x.

10 Nov 2011 Gergely  
10 Nov 2011 Gergely

Please consider the FIND command which is already built in..

length(find(Data==9))

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
matrices Richard Medlock 22 Oct 2008 07:00:21
count Richard Medlock 22 Oct 2008 07:00:21
elements Richard Medlock 22 Oct 2008 07:00:21
criteria Richard Medlock 22 Oct 2008 07:00:21
match Richard Medlock 22 Oct 2008 07:00:21
matrix Richard Medlock 22 Oct 2008 07:00:21
utilities Richard Medlock 22 Oct 2008 07:00:21
count THARSSHAN MANUEL 13 Apr 2009 17:35:11
count Fernando Duarte 31 Jul 2009 11:22:54
matrices Troy Pollard 20 Sep 2010 16:18:29

Contact us at files@mathworks.com