# will running a mean on the pdist of a matrix give me the avg of all distances of vectors, or just the elements of the vectors.

2 views (last 30 days)

Show older comments

Im trying to run a Euclidean distance between each row of vector values held in a 208x4096 matrix, eg fr every row of values,I want to calc distance to every other row in matrix, approx 43,000 Euclidean calculations, I then wish to get the mean of all those Euclidean Distances calculated. The code for the matrix distance calcs below, seems to be doing this but im unsure if it is giving me exactly what I am looking for as its such a high number of calculations, I cant think of how to check this, apart from asking a knowledgable Matlab user if the code looks right. Thank you in advance for any insight you can share

for i=1:length(allData)

dist(i,:)=pdist(allData(:,i),'euclidean')';

end

dist

mean(pdist(allData))

##### 0 Comments

### Answers (1)

Walter Roberson
on 26 Mar 2018

##### 2 Comments

Walter Roberson
on 26 Mar 2018

The first version will not work. You are extracting one column at a time and finding the distance between the entries in the column, which is just abs() of the difference between the two scalars. It would calculate each column in isolation. Which is not what you want: you want the euclidian distance between two rows.

You cannot just change the index around to select one row at a time. pdist would calculate the distance between the one row and itself, which would be 0.

pdist calculates every row against every other row. It returns a vector, which is a compact representation of the lower triangle (distance is symmetrical so you do not need to calculate A to B and B to A both). You can mean() the results to get a single overall mean.

Or instead of taking a single overall mean you can use squareform() to convert the compact triangle of distances into a full symmetric square of distances. That would permit you to calculate the average distance from individual rows to the other rows. I showed the code in the earlier posting including the bias correction that should be used.

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!