1

我的问题很简单,但我正在寻找它的矢量化形式。

我的代码是:

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);

编辑

目前我正在使用这个解决方案,它具有合理的性能:

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);
4

3 回答 3

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.
于 2013-02-19T09:35:52.527 回答
1

我同意Tal Darompdist2这正是您需要的功能。它找到两个向量中指定的每对坐标的距离,而不是两个矩阵之间的距离。

所以我很确定在你的情况下你想要这个:

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(:)])
于 2013-02-19T09:30:38.170 回答
0

你可以使用 matlab 函数pdist2(我认为它在统计工具箱中),也可以在线搜索该函数的开源良好实现。

另外,看看这个 unswer: pdist2 等效于 MATLAB 版本 7

于 2013-02-18T22:38:56.227 回答