pdist and squareform
Peter Hinow
hinow at uwm.edu
Wed Nov 11 19:28:19 CET 2009
Hello all,
the matlab functions pdist and squareform (from the statistics toolbox) are missing in scilab. So I wrote them myself and just want to know if the community has any use for it. The codes are pasted below. pdist is written with only the Euclidean distance in mind, I guess one can generalize it with an optional argument that specifies other metrics.
Best regards,
Peter
// this function computes the pairwise distances in the vector x
function [r] = pdist(x)
[rows,columns] = size(x);
if rows == 1
x = x'; swap = rows; rows = columns; columns = swap;
end
r = zeros(rows*(rows-1)/2,1);
start = 1;
for k = 1:rows-1
mat1 = kron(x(k,:),ones(rows-k,1)); mat2 = x(k+1:rows,:);
r(start:start+rows-k-1) = sqrt(sum((mat1-mat2).^2,2));
start = start+rows-k;
end
endfunction
// this function rearranges a vector of length n*(n-1)/2 in squareform (with zero diagonal)
function [r] = squareform(x)
[rows,columns] = size(x);
if rows == 1
x = x'; swap = rows; rows = columns; columns = swap;
end
m = (1+sqrt(1+8*rows))/2;
if abs(int(m)-m)>0.0001
disp('error: wrong dimension')
r=[-1];
return;
end
r=zeros(m,m);
start = 1;
for k = 1:m-1
r(k+1:m,k) = x(start:start+m-k-1); r(k,k+1:m) = x(start:start+m-k-1)';
start = start+m-k;
end
endfunction
More information about the users
mailing list