function dirtyLabels = classnoise(originalLabels, dirtyRatios)
% Create labels that are purposely dirtied with class noise. This
% class noise generator cannot control the direction of errors; it is
% always just "some other class, with equal probability".
% 
% In:
%  originalLabels - original integral class labels 1,2,...,K
%  dirtyRatios  - ratio of each class to destroy with class noise
%
% Out:
%  dirtyLabels    - integral class labels that are certainly wrong in
%                   dirtyRatios(k) first cases for each class k
%                   but otherwise equal to originalLabels.
%


% Find the different class labels present:
labs = sort(unique(originalLabels));

nclasses = max(labs);

assert((numel(labs) == numel(dirtyRatios)) ...
       && (min(labs)==1) && (nclasses==numel(labs)), ...
       'Verify parameters; something unexpected requested.')

dirtyLabels = originalLabels; % start with original ones.

% Destroy each class with the given percentage of certainly wrong ones:
for iic=1:numel(labs)
  ic = labs(iic);
  sel = find(originalLabels == ic); % indices belonging to this class
  selpart = sel(1:(dirtyRatios(ic)*numel(sel))); % indices to noisify
  wrong = [1:ic-1 ; ic+1:nclasses]; % any other class is "wrong"
  % Then just draw random, known-to-be wrong class labels:
  dirtyLabels(selpart)=wrong(randi([1 (nclasses-1)], 1, numel(selpart)));
end

