MATLAB: Compute distances between an element and all other elements of a matrix

distancesmatrix

Hello,
Please help me with the following:
I have a matrix M 150×4.
Ι need to calculate the distances (separately, the Euclidean and Cityblock) between the 1st element M(1,1) and all the others of M.
Also, I need to plot these distances.
Thank you very much.
Best,
Pavlos

Best Answer

If you have the statistics toolbox you can use pdist2
m=[1 1 1 1;2 2 2 2;3 3 3 3;4 4 4 4;5 5 5 5]
dist1=pdist2(m(1,:),m,'euclidean')
dist2=pdist2(m(1,:),m,'cityblock')
%plot index vs distance
plot(1:length(c),[dist1;dist2])