-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetMultiplicity.m
44 lines (36 loc) · 1020 Bytes
/
getMultiplicity.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
% [r, udata, sdata] = getMultiplicity(data) returns the occurrences/Multiplicity of the elements of 'data'
%
% Inputs:
% data : n-dimensional input array
%
% Outputs:
% rep : # of occurrences for each element of 'data'
% udata : sorted 1-D array of unique values in 'data'
% sdata : sorted 1-D array of values in 'data'
%
% Note: NaN/Inf elements in input data are ignored
% Francois Aguet, 03/02/2012 (modified on 10/29/2012)
% Mark Kittisopikul, calculate udata only when requested, 09/11/2014
function [rep, udata, sdata] = getMultiplicity(data)
% fail quickly if data is empty
if(isempty(data))
rep = [];
udata = data;
sdata = data;
return;
end
if(~isinteger(data))
data = data(isfinite(data));
end
% sort
sdata = sort(data(:));
% store as row vector
sdata = sdata(:)';
% find where the numbers change in the sorted array
isDiff = [diff(sdata)~=0 1];
idx = find(isDiff);
if(nargout > 1)
udata = sdata(idx);
end
% count occurrences
rep = diff([0 idx]);