Be the first to rate this file! 3 Downloads (last 30 days) File Size: 1.21 KB File ID: #25734

howmany Counts occurrencies of numbers in array

by Pekka Kumpulainen

 

04 Nov 2009

Counts the number of occurrences of finite values

| Watch this File

File Information
Description

% [val, num] = howmany(x)
%
% counts the number of occurrences of finite values in x
%
% in: x vector of nnumbers (array will be reshaped to a vector)
%
% out: val unique values in x, sorted
% num number of occurrences

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
04 Nov 2009 Nathan Greco

It would be nice if this checked for floating point differences as well.
Ex:
a = [0 0.1 0.2 0.3 0.4];
b = [0:0.1:0.4];
>> [v n] =howmany([a b])
%%%%%%%%%%%%%
v =
                    0
                    0.1
                    0.2
                    0.3
                    0.3
                    0.4
n =
     2
     2
     2
     1
     1
     2

Because the arrays a and b are essentially equal (within eps):
%%%%%%%%%%
>> abs(a-b)<eps
ans =
     1 1 1 1 1
>> isequal(a,b)
ans =
     0
%%%%%%%%%%
you should at least give an option to count them as the same number.

Just my two cents

-Nathan

12 Nov 2009 Pekka Kumpulainen

The exact equal match is intentional in this function. I have another function: "isclose" for the "close enough" comparisons.
But a good suggestion anyway, I might add that option.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
count numbers Pekka Kumpulainen 04 Nov 2009 09:20:46

Contact us at files@mathworks.com