function [pure, ind] = classifToClosest(original, prototypes, indOnly)
% Purifies a classification result by converting original data vector
% to exactly the closest prototype (Eucl. distance)
%
% (An old function, likely to be removed. It is easier to handle
% classification tasks by using class indices than by using class
% prototype vectors. So in the long run we'd better stick with the
% 'indOnly' usage type here. Optionally maybe give distance to
% prototype as the second return argument, to allow checking the 
% confidence of the answer)

for ii=1:size(prototypes,2)
  distI = original - repmat(prototypes(:,ii),1,size(original,2));
  normsq(ii,:) = sum(distI .^ 2, 1);
end

% Find indices corresponding to shortest distance from data point to
% prototype:
[dummy,ind] = min(normsq, [], 1);

% Quickly change my interface for today's purpose... 
if nargin==3
    if strcmp(indOnly,'indOnly')
        pure = ind;
        return
    end
end

% Build the purified representation:
pure = zeros(size(original));
for col=1:size(original,2)
  pure(:,col) = prototypes(:,ind(col));
end
