function nuv = findnuv(x,flag)
%   Function to create histogram of the number of occurrences of unique values in an array
%       nuv = findnuv(x,flag)
%   x       =       The input column vector
%   flag    =       The flag in the input vector
%   nuv     =       A two column array containing the unique values in input and the number of occurrences of each

is  = find(x~=flag);                        %Find the unflagged data in input vector
gx  = x(is);                                %The good data in input
gx  = sort(gx);                             %Sort the data in ascending order
[uv,i,j] = unique(gx);                      %Find the unique values
nu  = length(uv);                           %Total number of unique values
noc = repmat(0,nu,1);                       %Array to hold the number of each unique values
ia  = [0;i];                                %Augment the i index to include first point
noc = diff(ia);                             %Number of occurrences of each unique value
nuv = [uv,noc];                             %The output array with table of unique values and their # of occ
return

%Slow way of doing this
% LOOP TO FIND HOW MANY OCC OF EACH UNIQUE VALUE (Won't work for large, high resolution measurements)
for j = 1:nu                                %Loop for every unique value
    is  = find(x==uv(j));                   %Location of values equal to current unique value
    noc(j) = length(is);                    %Number of occurrences of current unique value
end
)
