how to select random rows from a matrix?

56 次查看(过去 30 天)
i have a matrix of size 10037 by 9.
please tell me how to select 1000 random rows from the matrix and save it as a mnew matrix in another variable.
thanks in advance.

回答(2 个)

Andrei Bobrov
Andrei Bobrov 2011-10-7
m = rand(10037,9);
k = randperm(10037);
mnew = m(k(1:1000),:);
  7 个评论
Jan
Jan 2017-10-24
Or with the original order:
M = rand(10037,9);
k = randperm(10037, 1000);
Selected = M(k, :);
r = true(1,10037);
r(k) = false;
Remaining = M(r, :);

请先登录,再进行评论。


Richard Willey
Richard Willey 2011-10-7
Statistics Toolbox includes a nice function called randsample
% Generate a matrix named foo
foo = randn(10000,2);
% draw 1000 random samples without replacement
index = randsample(1:length(foo), 1000);
bar = foo(index,:);
% draw 1000 random samples with replacement
index = randsample(1:length(foo), 1000, true);
bar = foo(index,:);
  2 个评论
Peter Perkins
Peter Perkins 2011-10-7
If you happen to be using R2011b, and have access to the Statistics Toolbox, you can also use the new datasample function:
m = rand(10037,9);
mnew = datasample(m,1000);
This also allows you to sample with replacement, or with weights.
Ege
Ege 2015-1-4
编辑:Ege 2015-1-4
And when we selected 1000 of them, how do we delete the selected rows from the original matrix which in this case foo ? And what does replacement mean in here?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by