function [dist] = distance(a, b)
% Function calculates the distances between points in two matrix. 
% Input
%   a   Matrix
%   b   Matrix
% Output
%   dist       Matrix that contains the distances.
%
% Authors: Anu Niemi, Annemari Auvinen

[am, an] = size(a); 
[bm, bn] = size(b);

if ((an ~= bn) | am ~=bm)
    error('Matrixes shoud be same shape');
end

sortedA = sortrows(a);
sortedB=sortrows(b);

dist(:,:)=sqrt((sortedA(:, :)-sortedB(:, :)).^2);