How can I select randomly?
2 次查看(过去 30 天)
显示 更早的评论
Hello, I have an 10000 rows and 10 columns matrix. I want to select randomly 500 rows from this matrix. I want to ask you, randperm function is true for this purpose. How can I select 500 rows randomly from this matrix?
0 个评论
采纳的回答
Jos (10584)
2012-12-12
Random can be defined in two ways:
% A is your original matrix
Nrows = size(A,1) ; % number of rows
% Option 1: randomly select 500 UNIQUE(!) rows
idx = randperm(Nrows) ;
idx = idx(1:500) ;
% Option 2: randomly select 500 rows
idx = randi(Nrows,[Nrows 1]) ;
% and then ...
B = A(idx,:) ;
2 个评论
更多回答(1 个)
Jan
2012-12-12
编辑:Jan
2012-12-12
M = rand(10000, 10);
index = randperm(10000, 500); % In modern Matlab versions
R = M(index, :);
In older Matlab versions randperm does not accept a 2nd input. Then:
index = randperm(10000);
index = index(1:500);
index = Shuffle(10000, 'index', 500)
7 个评论
Matt Fig
2012-12-14
Apparently saying, "M is your original matrix" would have made all the difference ;-).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!