Tuesday, 15 March 2011

matlab - Calculate distance, given a set of coordinates -



matlab - Calculate distance, given a set of coordinates -

my question quite trivial, i'm looking 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 using solution, 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);

i agree tal darom, pdist2 function need. finds distance each pair of coordinates specified in 2 vectors , not distance between 2 matrices.

so i'm pretty sure in case want this:

pdist2([y(:), z(:)], [y(:), z(:)])

the matrix [y(:), z(:)] list of every possible coordinate combination on 2d space defined y-z. if want matrix containing distance each point each other point must phone call pdist2 on matrix itself. result 2d matrix dimensions numel(y) x numel(y) , although haven't defined i'm pretty sure both y , z n*m matrices meaning numel(y) == n*m

edit: more right solution suggested @shai utilize pdist since comparing points within same matrix:

pdist([y(:), z(:)])

matlab vectorization euclidean-distance

No comments:

Post a Comment