from count (mex) by Stefano Gianoli
Counts the number elements in a specified vector or matrix that match a specified criteria.

test_count.m
NTIMES = 1000;
FINDWHAT = 11;
q = fix((FINDWHAT-2)*rand(565789,1));
q(1:10:12345) = FINDWHAT;


tic,
sum(q==FINDWHAT)
a=toc;
disp(['using sum 1 time ' num2str(a,'%12.6f')])



tic,
length(find(q==FINDWHAT))
a=toc;
disp(['using length 1 time ' num2str(a,'%12.6f')])

tic,
for i=1:NTIMES
sum(q==FINDWHAT);
end
a=toc;
disp(['using sum ' num2str(NTIMES) ' time ' num2str(a,'%12.6f')])

tic,
for i=1:NTIMES
length(find(q==FINDWHAT));
end
a=toc;
disp(['using length ' num2str(NTIMES) ' time ' num2str(a,'%12.6f')])

tic,
for i=1:NTIMES
r=(count(q,FINDWHAT,'=='));
end
a=toc;
disp(['using count ' num2str(NTIMES) ' time ' num2str(a,'%12.6f')])

Contact us at files@mathworks.com