I have a matrix X
of dimension r = 2 rows and col = 20000 columns and I want to compute the square root of the sum of squared distances = Euclidean distance between pair of points. For ex:
Let,
X = 1 2 3 4
5 6 7 8
Dist1 = sqrt((1-2)^2 + (5-6)^2))
Dist2 = sqrt((1-3)^2 + (5-7)^2))
and so on. So, distance(1,2) = Dist1;
distance(1,3) = Dist2
The result will be a matrix of size N*N
.
But, it is taking a lot of time when the data points are large say 1 million. How can I effectively modify this code so that it is decent and fast. Please help.
r =2;
col = 2000;
X = rand(r,col);
N = col;
for k =1: N
for l = 1: N
if (l ~= k)
distance(k,l) =( sqrt(sum((X(:,k) - X(:,l)) .^ 2)));
end
end
end
end