随机删除行

时间:2013-10-08 12:42:05

标签: matlab random matrix

我需要选择 i W_tot_migl矩阵的随机行。现在我这样做:

[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 个答案:

答案 0 :(得分: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,:) = [];

请注意,当您一次性选择它们时,您无需担心重复。如果这是从原始版本删除它们的唯一原因,那么最后一行现在已经过时了。