Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Calculate distance matrix
Replies: 3   Last Post: Jun 4, 2013 10:10 AM

 Messages: [ Previous | Next ]
 Francesco Perrone Posts: 39 Registered: 5/2/12
Calculate distance matrix
Posted: Jun 4, 2013 7:51 AM

Hi everybody,

I hope you will have the right answer to this question, which is rather challanging.

I have two uni-dimensional vectors Y and Z, which hold the coordinates of N grid points located on a square grid. So

Ny points along Y
Nz points along Z
N = Ny*Nz
Y = Y[N]; (Y holds N entries)
Z = Z[N]; (Z holds N entries)

Now, the goal would be generating the distance matriy D, which holds N*N entries: so each row of matrix D is defined as the distance between the i-th point on the grid and the remnant (N - i) points.

Generally, to compute the whole matrix I would call

D = squareform(pdist([Y Z]));
or,equivalently,
D = pdist2([Y Z],[Y Z]).

But, since D is a square matrix, I'd like to generate only the N(N + 1)/2 independent entries and store them into a uni-dimensional vector Dd.

So the question is: how to generate a uni-dimensional array Dd whose entries are the defined by the lower traingular terms of matrix D? I'd, furthermore, like storing the entries in a column-major order.

I hope the explanation is clear enough.