221b
221b

Reputation: 431

Calculate Euclidean distance for every row with every other row in a NxM matrix?

I have a matrix that I generate from a CSV file as follows:

X = xlsread('filename.csv');

I am looping through the matrix based on the number of records and I need to find the Euclidean distance for each of the rows of this matrix :

for i = 1:length(X)
 j = X(:, [2:5])
end

The resulting matrix is of 150 X 4. What would be the best way to calculate the Euclidean distance of each row (with 4 columns as the data points) with every row and getting an average of the same?

Upvotes: 0

Views: 601

Answers (1)

Miriam Farber
Miriam Farber

Reputation: 19634

In order to find the Euclidean distance between any pair of rows, you could use the function pdist.

X = randn(6, 4);
D = pdist(X,'euclidean');
res=mean(D);

The average is stored in res.

Upvotes: 1

Related Questions