MATLAB: How to find euclidean distance between training and test data

for loopvectors

I have two matrices A of size 2×5 and B of size 2×2 such that each column is a feature vector. I want to calculate the euclidean distance between A and B. i.e I want to calculate the euclidean distance between first column of B with every column of A and similarly need to calculate the second column of B with every column of A .
A=rand(5,2);
B=rand(2,2);
for i=1:2
for j=1:5
d=sqrt(B(i,:)-A(j,:));
end
end

Best Answer

Statistics toolbox:
d = pdist2(A.', B.');
pdist2 normally operates row by row, which is why the .' are there, to make your column-oriented data into row-oriented data.
Related Question