Use pdist2
. Assuming (from your code) that your vectors are columns, transposition is needed because pdist2
works with rows:
[cmin, match_col] = min(pdist2(vectors.', input_vector.' ,'euclidean'));
It can also be done with bsxfun
(in this case it's easier to work directly with columns):
[cmin, match_col] = min(sum(bsxfun(@minus, vectors, input_vector).^2));
cmin = sqrt(cmin); %// to save operations, apply sqrt only to the minimizer