I think pdist2
is a good option, so I help you to define your distance function.
According to the doc, the self-defined distance function must have 2 inputs: first one is a 1-by-N vector; second one is a M-by-N matrix (be careful of the order!).
To avoid the use of repmat
which is indeed memory-consumant, you can use bsxfun
to apply some basic operations on data with expansion over singleton dimensions. In your case, you can do the following thing:
distance_kernel = @(x,Y) sum(bsxfun(@min,x,Y),2);
Summation is done over the columns to get a column vector as output.
Then just call pdist2
and you are done.