1

my question is quite trivial, but I'm looking for the vectorized form of it.

My code is:

HubHt =  110; % Hub Height
GridWidth =  150; % Grid length along Y axis
GridHeight =  150; % Grid length along Z axis
RotorDiameter =  min(GridWidth,GridHeight); % Turbine Diameter
Ny =  31;
Nz =  45;
%% GRID DEFINITION

dy = GridWidth/(Ny-1);
dz = GridHeight/(Nz-1);
if isequal(mod(Ny,2),0)
    iky = [(-Ny/2:-1) (1:Ny/2)];
else
    iky = -floor(Ny/2):ceil(Ny/2-1);
end

if isequal(mod(Nz,2),0)
    ikz = [(-Nz/2:-1) (1:Nz/2)];
else
    ikz = -floor(Nz/2):ceil(Nz/2-1);
end

[Y Z] = ndgrid(iky*dy,ikz*dz + HubHt);

EDIT

Currently I am using this solution, which has reasonable performances:

coord(:,1) = reshape(Y,[numel(Y),1]);
coord(:,2) = reshape(Z,[numel(Z),1]);
dist_y = bsxfun(@minus,coord(:,1),coord(:,1)');
dist_z = bsxfun(@minus,coord(:,2),coord(:,2)');
dist = sqrt(dist_y.^2 + dist_z.^2);
fpe
  • 2,700
  • 2
  • 23
  • 47
  • @Dan: I just edited my question again. Honestly, there was an evident flaw in the previous edition of the question concerning how I want to define distance. – fpe Feb 19 '13 at 08:13
  • @TalDarom is correct, pdist2 is correct. [link](http://www.mathworks.com/help/stats/pdist2.html) from the docs it claims to find the pairwise distance between each element of 2 matrices – Dan Feb 19 '13 at 08:46
  • `pdist2` is absolutely not the answer: I don't want to calculate the distance between the two matrices, but distance among points on the grid, where `Y` and `Z` are representative of the cartesian coordinates of each point on the grid. – fpe Feb 19 '13 at 09:04
  • pdist2 is absolutely the answer - I'm not saying pdist2(Y, Z) is the anzwer but pdist2 is the function. See my answer for what arguments to pass to it. It does not find the distance between 2 matrices, it finds the distance between every two point combinations of two lists of vectors which in your case will be the same 2 lists which are just Y and Z flattened and concatened. – Dan Feb 19 '13 at 09:33

3 Answers3

2

I disagree with Dan and Tal.

I believe you should use pdist rather than pdist2.

D = pdist( [Y(:) Z(:)] ); % a compact form
D = squareform( D ); % square m*n x m*n distances.
Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
1

I agree with Tal Darom, pdist2 is exactly the function you need. It finds the distance for each pair of coordinates specified in two vectors and NOT the distance between two matrices.

So I'm pretty sure in your case you want this:

pdist2([Y(:), Z(:)], [Y(:), Z(:)])

The matrix [Y(:), Z(:)] is a list of every possible coordinate combination over the 2D space defined by Y-Z. If you want a matrix containing the distance from each point to each other point then you must call pdist2 on this matrix with itself. The result is a 2D matrix with dimensions numel(Y) x numel(Y) and although you haven't defined it I'm pretty sure that both Y and Z are n*m matrices meaning numel(Y) == n*m

EDIT:
A more correct solution suggested by @Shai is just to use pdist since we are comparing points within the same matrix:

pdist([Y(:), Z(:)])
Community
  • 1
  • 1
Dan
  • 45,079
  • 17
  • 88
  • 157
  • 1
    Adjusted to use pdist rather than pdist2 as suggested by shai. This is more correct. – Dan Feb 19 '13 at 09:55
0

You can use the matlab function pdist2 (I think it is in the statistics toolbox) or you can search online for open source good implementations of this function.

Also, look at this unswer: pdist2 equivalent in MATLAB version 7

Community
  • 1
  • 1
Tal Darom
  • 1,379
  • 1
  • 8
  • 26
  • I don't want the distance between the two matrices: I already know `pdinst2`. I will anyway give a better read to it. – fpe Feb 18 '13 at 22:43