2013-05-30 115 views
0

假設我有一個矩陣A,它是一個n * 3矩陣,列出了n個點的笛卡爾座標。我感興趣的是找到北極和南極,它們基本上是兩個座標,最大距離相隔。我想要northPole和southPole分別有最小和最大z座標,我想用x和y座標來打破關係。這個問題很容易使用循環執行,但我想讓我的代碼有效,因爲矩陣A真的很大。因此,我需要使用內置的MatLab函數來尋找northPole和southPole,以便代碼高效。謝謝!使用MatLab找到最大距離

+1

'NORTHPOLE和southPole這兩個基本的座標與最大距離apart'這可能會爲你的數據是真實的,但你要明白,這個不具有普遍持有? –

+0

「我想要northPole和southPole分別有最小和最大z座標」 - 所以爲什麼不在z座標上使用'max'和'min'? –

+0

@DavidK,因爲他想「打破關係」 – Shai

回答

0

您自己描述了答案:首先找到最小或最大Z值的索引,然後使用最小的X距離來打破關係。

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) 
+0

我不確定您的d2計算是否正在尋找。找到zmaxNdx和zminNdx後,我怎麼做:sqrt((x2-x1)^ 2 +(y2-y1)^ 2)),然後找到最小化這個表達式的點。 –

+0

@SahilChaudhary這幾乎是我正在使用的公式。我假設在極點x == y == 0,所以'x1'和'y1'將爲零。我也省略了'sqrt()'作爲一個優化,因爲你只需要找到最小值並且平方根不會改變,這是最小的。 – shoelzer

+0

嘿,請閱讀我發佈的答案,我遇到了問題。非常感謝! :) –

0

你提出的算法實際上並不能找到兩個座標間的最大距離。例如,如果距離最遠的兩個點有z = 0

這裏有兩個解決方案,一個實際上會給你兩個距離最遠的點,另一個是你的NorthPole-southPole算法的實現。請注意,在這兩種解決方案中,我給出的函數都會返回候選點的索引,而不是點本身。您可以通過

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

解決方案1獲得積分自己:找到兩個點與最大距離相隔

嘗試使用distmat功能從file exchange.這需要一個N x D陣列點A的,其中N是並且D是尺寸,並且返回N x N陣列的距離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 
+0

嘿,請閱讀我發佈的答案,我有問題。非常感謝! :) –

+0

我用兩種可能的解決方案編輯了我的答案。 – hoogamaphone