For me histc(x,unique(x)) did what I wanted. This gives me the frequency of each value that occurs in the vector x.
23 Mar 2007
olcay onder
Thanks for help.t's very usefull.
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
08 Jun 2006
Anil Ramachandran
dude, just use size(find(index == i),1) to find the number of times i exists in index.
18 Feb 2006
Jos van der Geest
Poor help and worse programming style. This file should be removed.
08 Sep 2005
Dimitri Shvorob
Duplicates built-in FIND.
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...
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.
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
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.