2013-10-08 70 views
1

我需要選擇i矩陣的隨機行。現在我在做這樣的:隨機刪除行

[m, n] = size(W_tot_migl); % m data points, n dimensions 
randomPoints = []; 
for i=1:14250 
    index = random('unid', m); % Pick the index at random. 
    randomPoints(i,:) = W_tot_migl(index,:); % Add random point. 
    W_tot_migl(index,:) = []; % Delete selected row. 
    m = m-1; 
end 

有一個更快的方法,也許避免了循環?

回答

1

這是一個比較MATLAB的方式來做到這一點:

nr = 5; %How many do you want to pick 

n = size(W_tot_migl,1); 
idx = randperm(n,nr); 

randomPoints = W_tot_migl(idx,:); 
W_tot_migl(idx,:) = []; 

注意,隨你挑他們一下子,你不需要擔心重複。如果這是從原始文件中刪除它們的唯一原因,則最後一行現在已經過時。

+1

請注意,對於(不是太老)的Matlab版本'randperm'只承認第一個參數。在這種情況下,你可以使用:'idx = randperm(n); idx = idx(1:nr);' –