フィルターのクリア

How can I select randomly?

1 回表示 (過去 30 日間)
Selin Soguksu
Selin Soguksu 2012 年 12 月 12 日
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?

採用された回答

Jos (10584)
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 件のコメント
Selin Soguksu
Selin Soguksu 2012 年 12 月 12 日
Thank you very much for the answer. :)) It works great. But I want to ask you a question more. You wrote "randomly select 500 UNIQUE(!) rows", in this way for example when 550th row is selected, no more it can be selected. Is it true?
Jan
Jan 2012 年 12 月 12 日
編集済み: Jan 2012 年 12 月 12 日
The RANDPERM approach creates unique indices, while the indices created by RANDI can be non-unique, e.g. [1, 503, 1017, 1, ...].

サインインしてコメントする。

その他の回答 (1 件)

Jan
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);
If this must be fast, use the C-Mex FEX: Shuffle.
index = Shuffle(10000, 'index', 500)
  7 件のコメント
Matt Fig
Matt Fig 2012 年 12 月 14 日
Apparently saying, "M is your original matrix" would have made all the difference ;-).
Selin Soguksu
Selin Soguksu 2012 年 12 月 19 日
I understand :)))

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by