2014-03-03 120 views
0

我已經在羣集周圍繪製多邊形方面做了大量的閱讀,並且意識到可能是最好的前進方向。基本上我正在尋找一個彈性像多邊形來包裹我的集羣點。用於數據中多個羣集的凸殼/凹殼

我的數據是由x(第1列)和y(第2列)組成的矩陣組成的矩陣(第3列)。我有700個這樣的集羣,因此不可能分別繪製每個集羣。

是否有辦法分別對每個羣集執行convhull,然後將它們中的每一個繪製在單個圖表上。

enter image description here

編輯

我已經寫到現在爲止這是不能夠每一個人在集羣上運行的凸包代碼...

[ndata, text, alldata] = xlsread(fullfile(source_dir)); 

     [~, y] = sort(ndata(:,end)); 
     As = ndata(y,:); 

     lon = As(:,1); 
     lat = As(:,2); 
     cluster = As(:,3); 

     %% To find number of points in a cluster (repetitions) 
    rep = zeros(size(cluster)); 

    for j = 1:length(cluster) 
     rep(j) = sum(cluster==cluster(j)); 
    end 

    %% Less than 3 points in a cluster are filtered out 

    x = lon (rep>3); 
    y = lat (rep>3); 
    z = cluster (rep>3); 

    %% convex hull for each cluster plotted ....hold....then display all. 

    figure 
    hold on 

    clusters = unique(z); 

     for i = 1:length(z) 

      k=convhull(x(z==clusters(i)), y(z==clusters(i))); 

      plot(x, y, 'b.'); %# plot cluster points 
      plot(x(k),y(k),'r-'); %# plots only k indices, giving the convex hull 


     end 

下面的圖像顯示什麼;

如果這個問題已經被問到,我很抱歉重複,但請直接給我你的答案,你會看到適合。

請任何人都可以幫忙,無論如何,我真的很掙扎! enter image description here

+0

好像它應該是足夠簡單,如果你將數據分割成700套的數據點(每個簇)。對每個羣集運行凸包算法,然後繪製生成的船體。 – Nuclearman

+0

@Nuclearman是絕對的,但我對matlab並不擅長,並且無法實現它。我寫了一些代碼,但它爲整個數據集而不是組實現了凸包。不知道如何分別爲每個羣集實施它。編輯我的問題來添加我現在的代碼。 – user3299469

+0

我想我應該預料到缺乏熟悉的問題。這是一個問題,因爲我不熟悉matlab它(因此是一個評論,而不是答案)。但是,您應該可以通過按羣集對數據進行排序來按羣集對數據進行分組。然後使用for循環遍歷每個數據點並將其添加到該羣集中的點列表,直到羣集更改。發生這種情況時,請構建並繪製上一個集羣列表的凸包,並創建一個新列表,並將當前點作爲下一個集羣列表的開始。只要確保你繪製了最後一個集羣。 – Nuclearman

回答

1

我將遍歷所有的集羣並執行您已經寫好的內容,並使用hold on選項累積同一圖中的所有圖。事情是這樣的:

% Generate three clouds of points in 2D: 
c1 = bsxfun(@plus, 0.5 * randn(50,2), [1 3]); 
c2 = bsxfun(@plus, 0.6 * randn(20,2), [0 0]); 
c3 = bsxfun(@plus, 0.4 * randn(20,2), [1 1]); 

data = [c1, ones(50,1); ... 
     c2, 2*ones(20,1); ... 
     c3, 3*ones(20,1)]; 

% Plot the data points with different colors 

clf 
plot(c1(:,1), c1(:,2),'r+', 'LineWidth', 2); 
hold on 
plot(c2(:,1), c2(:,2),'k+', 'LineWidth', 2); 
plot(c3(:,1), c3(:,2),'b+', 'LineWidth', 2); 

x = data(:,1); 
y = data(:,2); 
cluster = data(:,3); 

clusters = unique(cluster); 

for i = 1:length(clusters) 
    px = x(cluster == clusters(i)); 
    py = y(cluster == clusters(i)); 
    if length(px) > 2 
     k = convhull(px, py); 
     plot(px(k), py(k), '-'); 
    end 
end 

它提供了以下結果: 2D Clusters and their corresponding Convex Hull

+0

非常感謝你,我理解這個腳本背後的邏輯,但是當我用我的數據運行它時,我得到一個錯誤?錯誤使用==> convhull 計算凸包的錯誤。沒有指定足夠的唯一點。 錯誤==> cluster_polygon at 21 k = convhull(x(cluster == clusters(i)),y(cluster == clusters(i))); – user3299469

+0

是的,它接縫你至少需要3個不同的點,但可以通過例如,如果檢查長度(cluster ==簇(i))> 2,如果不是,然後做其他事情(如不繪製凸包或者只是繪製由兩個點定義的線..) –

+0

感謝您的編輯,正如您所建議的,我已經使用少於3個點的方式移除了這些羣集。但是代碼仍然沒有達到我的預期。我已經添加了一個圖像來解釋它當前顯示的內容。你能幫忙嗎?非常感謝你。 – user3299469