2014-10-28 210 views

回答

4

Ander's answer是相當不錯的,但我有一些保留

  1. randi可以返回相同的指數超過一次。例如,

    >> randi(10,1,5) 
    ans = 
    6  3 10  6  2 
    

    返回6兩次。因此它可以減少來自矩陣的減去n元素。

  2. 選擇哪些元素應該保留在矩陣中比構造完整的矩陣並丟棄其中的元素更有效:您實際上在每個命令處複製大部分矩陣。

因此,我的解決方案將使用randsample

N = size(adj,1); %// current number of nodes 
toKeep = N - n; %// n is number to remove 
idx = randsample(N, toKeep); %// sample WITHOUT replacement 
newadj = adj(idx, idx); %// copy only the relevant elements 
+2

你絕對是個好人,我寫得不是太多,而且我寫得很短!感謝您的好回答:) – 2014-10-29 16:37:40

2

編輯:@Shai's answer比我的更好,並顯示我的錯誤。這是真正的好答案。

如果我沒有錯:鄰接矩陣是一個Npoints X Npoints矩陣,每個節點顯示哪個節點是相鄰的。

我想如果你想刪除隨機點,你需要刪除該點的行和列。

% n is number of points that you want to delete 
% adj is the adjacency matrix 
idx=randi(size(adj,1),n) 
newadj=adj; 
newadj(idx,:)=[]; 
newadj(:,idx)=[]; 
+0

我得到這個錯誤:在等號左邊的表達式簽名不是分配一個有效的目標。 – Arash 2014-10-28 19:06:14

+0

對此:auxadj = adj(idx,:)= []; newadj = auxadj(:,idx)= []; – Arash 2014-10-28 19:07:08

+0

@arashams我的錯誤 – 2014-10-28 20:31:31