3.0

3.0 | 1 rating Rate this file 1 Download (last 30 days) File Size: 4.75 KB File ID: #5194

count (mex)

by Stefano Gianoli

 

10 Jun 2004 (Updated 11 Jun 2004)

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

| Watch this File

File Information
Description

Given a vector or matix MATRIX, counts the number of times that the scalar X satisfy the (string) condition COND. The result is returned in the variable RES. the COND operators allowed are: ==, >=, <=, <, >, !=. The function is faster than SUM(MATRIX'COND'X) or LENGTH(FIND(MATRIX'COND'X)). A test script is provided to show the performances.

MATLAB release MATLAB 6.5 (R13)
Other requirements Tested on XP,R13
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
18 Feb 2006 Jos van der Geest

On my PC it is indeed about 4 times faster than the better known <sum> solution. However, it does not handle NaNs very well.
count([1 2 NaN],1,'==') -> ans = 2
as opposed to
sum([1 2 NaN]==1) -> ans = 1

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
matrices Stefano Gianoli 22 Oct 2008 07:23:18
count Stefano Gianoli 22 Oct 2008 07:23:18
mex Stefano Gianoli 22 Oct 2008 07:23:18
matrix manipulation Stefano Gianoli 22 Oct 2008 07:23:18
utilities Stefano Gianoli 22 Oct 2008 07:23:18
matrix Stefano Gianoli 22 Oct 2008 07:23:18

Contact us at files@mathworks.com