how to select random rows from a matrix?

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일

9 개 추천

m = rand(10037,9);
k = randperm(10037);
mnew = m(k(1:1000),:);

댓글 수: 7

N
N 2011년 10월 7일
thanks, but i want to select 1000 random rows from the matrix 'f' that i already have in hand. please help me.
Andrei Bobrov
Andrei Bobrov 2011년 10월 7일
편집: Walter Roberson 2024년 3월 10일
k = randperm(size(f,1));
mnew = f(k(1:1000),:);
so?
Jan
Jan 2011년 10월 7일
편집: Walter Roberson 2024년 3월 10일
And since Matlab 2011b:
mnew = f(randperm(size(f, 1), 1000), :);
abishek dixit
abishek dixit 2017년 10월 24일
what if i want to keep that matrix as well from which the rows were extracted. i mean the matrix with remaining 37 rows.
Along with Andrei's code, use
mnew_rest = f(k(1001:end),:);
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, :);
Alejandro Reyes
Alejandro Reyes 2024년 3월 10일
This works. Thank you!

댓글을 달려면 로그인하십시오.

Richard Willey
Richard Willey 2011년 10월 7일

4 개 추천

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?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

N
N
2011년 10월 7일

편집:

2024년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by