更新时间:2022-10-26 09:28:50
pdist
确实如此那。 squareform
是需要以正方形对称矩阵的形式得到结果:
$ $ $ $ $ $ c $ dist dist = squareform(pdist(cord。')) ;
I have a 3 X 1000 (and later 3 X 10 000) matrix cord
given, which contains the three dimensional coordinates for my pixels.
My intention is to calculate the distance between all the pixels, and I do it with a for loop (see below), but I will have to calculate this for huge matrices soon, and am wondering if I could vectorize the code for making it faster...?
dist = zeros(size(cord,2),size(cord,2));
for i = 1:size(cord,2)
for j = 1:size(cord,2)
dist(i,j) = norm(cord(:,i)-cord(:,j));
dist(j,i) = dist(i,j);
end
end
pdist
does exactly that. squareform
is needed to get the result in the form of a square, symmetric matrix:
dist = squareform(pdist(cord.'));