【问题标题】:finding maximum distance using MatLab使用 MatLab 找到最大距离
【发布时间】:2013-05-30 15:16:21
【问题描述】:

假设我有一个矩阵A,它是一个 n*3 矩阵,列出了 n 个点的笛卡尔坐标。我感兴趣的是找到北极和南极,它们基本上是两个相距最大的坐标。我希望北极和南极分别具有最小和最大 z 坐标,并且我想使用 x 和 y 坐标来打破平局。使用循环很容易解决这个问题,但我想让我的代码高效,因为矩阵 A 真的很大。因此,我需要帮助使用内置的 MatLab 函数查找北极和南极,以便代码高效。谢谢!

【问题讨论】:

  • northPole and southPole which are basically two coordinates with maximum distance apart 这可能适用于您的数据,但您确实意识到这通常不必成立?
  • “我希望北极和南极分别具有最小和最大 z 坐标” - 那么为什么不在 z 坐标上使用 maxmin 呢?
  • @DavidK 因为他想“打破僵局”
  • 你能保证距离最大的两个坐标分别有最小和最大z坐标吗?或者你想先选择最大和最小 z 坐标,然后在这个子集中找到它们之间距离最大的两个点?
  • 我想先选择 max 和 min z 的坐标,然后用 x 和 y 来决定极点。

标签: matlab function


【解决方案1】:

您自己描述了答案:首先找到最小或最大 Z 值的索引,然后使用最小的 X 和 Y 距离来打破平局。

zmax = max(A(:,3));
zmaxNdx = find(zmax == A(:,3));
d2 = A(zmaxNdx,1).^2 + A(zmaxNdx,2).^2;
[~, d2minNdx] = min(d2);
northpoleNdx = zmaxNdx(d2minNdx)

zmin = min(A(:,3));
zminNdx = find(zmin == A(:,3));
d2 = A(zminNdx,1).^2 + A(zminNdx,2).^2;
[~, d2minNdx] = min(d2);
southpoleNdx = zminNdx(d2minNdx)

【讨论】:

  • 我不确定您对 d2 的计算是否是我正在寻找的。在找到 zmaxNdx 和 zminNdx 之后,我这样做怎么样:sqrt((x2-x1)^2 + (y2-y1)^2)) 然后找到最小化这个表达式的点。
  • @SahilChaudhary 这几乎就是我使用的方程式。我假设两极处的 x == y == 0,所以 x1y1 将为零。我还省略了sqrt() 作为优化,因为您只需要找到最小值并且取平方根不会改变哪个最小。
  • 嘿,请阅读我发布的答案,我遇到了问题。非常感谢! :)
【解决方案2】:

您提出的算法实际上不会找到相距最大距离的两个坐标。例如,如果距离最大的两个点有z = 0 怎么办?

这里有两个解决方案,一个实际上会给你两个最大距离的点,另一个是你的北极-南极算法的实现。请注意,在这两种解决方案中,我给出的函数都返回候选点的索引,而不是点本身。您可以自己获得积分

north = A(iN, :);
south = A(iS, :);

解决方案 1:找到距离最大的两个点

尝试使用file exchange. 中的distmat 函数它需要一个N x D 点数组A,其中N 是点数,D 是维度,并返回一个@ 987654329@ 距离数组dist,其中dist(i, j)A(i,:)A(j,:) 之间的距离。 distmat 有 4 种不同的算法可用于生成矩阵,具体取决于哪种算法可以提供最佳性能。

您可以使用此函数创建一个距离矩阵,然后使用max 定位最大距离的候选者。然后你可以用任何你喜欢的方式打破关系。

在下面的函数中,我找到了所有距离最大的点。如果有不止一对,我会根据候选者之间的 x-y 距离打破平局。

function [iN, iS] = poles(A)

% create the distance matrix
dist = distmat(A);

% find all candidate pairs of north and south poles
[~, iMax] = max(dist(:));
[iN, iS] = ind2sub(size(dist), iMax);

% If there is only one, you are done, otherwise break the ties.
if length(iMax) == 1
    return
end

%
% break ties by the euclidean distance of the x-y coordinates
% note that this may not result in a unique set of north and south poles,
% but you can always break further ties.
north = A(iN, 1:2);
south = A(iS, 1:2);

tieBreak = sum(abs(north-south).^2, 2);

[~, iMax] = max(tieBreak);

iN = iN(iMax);
iS = iS(iMax);
end

解决方案2:找到“北极”和“南极”

此解决方案还使用distmat 函数。首先,它找到具有最大和最小 z 值的点。如果没有关系,那么它就会停止。否则,它会找到具有最大距离的一对“北”和“南”点,并如上所述进行抢七。

function [iN, iS] = poles(A)

% find the min and max z values
[~, iMaxZ] = max(A(:, 3));
[~, iMinZ] = min(A(:, 3));
iMaxZ = iMaxZ(:);
iMinZ = iMinZ(:);

if length(iMaxZ) == 1 && length(iMinZ) == 1
    iN = iMaxZ;
    iS = iMinZ;
    return
end

nMax = length(iMaxZ);
nMin = length(iMinZ);

% put the north and south poles together
northSouth = A([iMaxZ; iMinZ], :);

% find the distance between them
dist = distmat(northSouth);

% restrict to only north-south pairs
dist = dist(1:nMax, nMax+1:end);

% find the maximum distance
[~, iMaxDist] = max(dist(:));
[iN, iS] = ind2sub(size(dist), iMax);

if length(iMaxDist) == 1
    return
end

% break ties by the euclidean distance of the x-y coordinates
% note that this may not result in a unique set of north and south poles,
% but you can always break further ties.
north = A(iN, 1:2);
south = A(iS, 1:2);

tieBreak = sum(abs(north-south).^2, 2);

[~, iMax] = max(tieBreak);

iN = iN(iMax);
iS = iS(iMax);
end

【讨论】:

  • 嘿,请阅读我发布的答案,我遇到了问题。非常感谢! :)
猜你喜欢
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 2014-10-04
  • 2013-01-28
  • 1970-01-01
  • 2017-08-03
  • 2011-09-06
  • 1970-01-01
相关资源
最近更新 更多