Question: I am computing a similarity matrix based on Euclidean distance in MATLAB. My code is as follows:

```
for i=1:N % M,N is the size of the matrix x for whose elements I am computing similarity matrix
 for j=1:N
  D(i,j) = sqrt(sum(x(:,i)-x(:,j)).^2)); % D is the similarity matrix
 end
end

```

Can any help with optimizing this = reducing the for loops as my matrix `x` is of dimension `256x30000`.

Thanks a lot!

--Aditya

Answer: 
The function to do so in matlab is called pdist. Unfortunately it is painfully slow and doesnt take Matlabs vectorization abilities into account.

The following is code I wrote for a project. Let me know what kind of speed up you get.

```
   Qx=repmat(dot(x,x,2),1,size(x,1));
   D=sqrt(Qx+Qx'-2*x*x');

```

Note though that this will only work if your data points are in the rows and your dimensions the columns. So for example lets say I have 256 data points and 100000 dimensions then on my mac using x=rand(256,100000) and the above code produces a 256x256 matrix in about half a second.
There's probably a better way to do it, but the first thing I noticed was that you could cut the runtime in half by exploiting the symmetry `D(i,j)==D(i,j)`

You can also use the function `norm(x(:,i)-x(:,j),2)`