0

For example, given X, a list N of points in R^d, (Nxd matrix). Then X = [x_1;x_2;...;x_N]. I want to compute the distance matrix D (and NxN matrix) between points {x_i}.

If d = 1 I can use bsxfun:

D = bsxfun(dist,X,X')

Where dist is defined on scalars by dist = @(x_i,x_j) sqrt( sum((x_i-x_j).^2) ). For d = 1, dist takes in two scalars and outputs a scalar, so bsxfun may be applied.

What I want to do is to have this make since for general d. That is, I need a function like bsxfun that can work when dist takes in two vectors and outputs a scalar.

Does anyone have any ideas?

student
  • 1
  • 1
  • For distances, have you checked `pdist2`? – Luis Mendo Jul 16 '14 at 09:17
  • 1
    It would be more clear if you included an example. Are you saying you want to take an n-by-d matrix, and find the distance for each pair of rows resulting in a symmetrical n-by-n (i.e. what `pdist2` does)? – Dan Jul 16 '14 at 09:18
  • pdist2 would solve the example I mentioned, but I would ultiamtely like this to work whenever I expand XX^T where X is a vector that may contain non numerical information. – student Jul 16 '14 at 09:23
  • Why don't you use a for loop? The for loop could also be included in the "dist" function, if you nest its definition (or if you put it in a separate file) – hyamanieu Jul 16 '14 at 09:28
  • These solutions might be relevant - http://stackoverflow.com/a/23911671/3293881 and http://stackoverflow.com/a/24345847/3293881 – Divakar Jul 16 '14 at 09:37
  • @student you may also find this solution http://stackoverflow.com/a/17091071/1714410 inspiring. – Shai Jul 16 '14 at 12:02

0 Answers0