# Is there a function that will allow me to convert a Euclidean distance calculation for two vectors in a matrix to all the vectors in the matrix?

1 view (last 30 days)
Alexander Eaton on 24 Mar 2018
Commented: Alexander Eaton on 25 Mar 2018
The code I have currently converts any two rows of 4096 image vectors within a matrix which I have named allData. e.g.
diffs = allData(1,:) - allData(2,:) % 1 being the first row of vectors...
diffsSq = diffs .*diffs
sqrt(sum(diffsSq))
Is there a Matlab function that can maintain this calculation and iterate over all the image rows comparing each with each and then allow me to take the average(mean) of the result. I know thats a lot of calculations but hopefully it will make my result more accurate than just comparing distance between two images.
I suppose the mean is just one line of code e.g. result = mean(allData,dist) or something to that effect.
I appreciate any guidance you can share, thank you

Von Duesenberg on 25 Mar 2018
If I understand your question correctly, this should do the trick:
mean(pdist(allData))

Walter Roberson on 25 Mar 2018
Note this would create a single global mean over everything, rather than a mean per row.
To get a mean per row, I would suggest
sum( squareform(pdist(allData)) ) ./ (size(allData,1)-1)
Taking the sum and dividing by rows minus 1 compensates for the fact that you likely do not want to include the diagonal of 0s caused by comparing each row to itself. If you do not care about that correction then
mean( squareform(pdist(allData)), 2)
to get a mean per row. This would be equivalent to
mean( squareform(pdist(allData))) .'
Alexander Eaton on 25 Mar 2018
thank you very much, I think once I find a way to calculate Euclidean distance over all rows, then the global mean may be the answer I require to compare against other data, but it would also be handy to have the function that can create a mean over each row and shown as a graph. I appreciate very much the help guys, thank you.
Alexander Eaton on 25 Mar 2018
sorry if I only upvoted one answer here,dont know how I can do both, they were both very helpful.