フィルターのクリア

Randomly select samples from a matrix in proper order

9 ビュー (過去 30 日間)
Shuo Zhang
Shuo Zhang 2018 年 1 月 25 日
コメント済み: Shuo Zhang 2018 年 1 月 26 日
I have a 1 by 30000 matrix and I'd like to randomly select 300 elements from it. However, the selection should follow such a rule that if the previous selected element is the nth element in the matrix, then the next selection should be at least the n+1th element in the matrix. For example, if the first selection is the 5th element in the matrix, then the next selection should be done from the 6th element to the 30000th element, etc. I found out there's a command 'y = datasample(data,k)' that can take samples randomly, but what else should I do to ensure the right order?

回答 (2 件)

James Tursa
James Tursa 2018 年 1 月 26 日
編集済み: James Tursa 2018 年 1 月 26 日
Solution without repeats:
x = your vector
n = number of samples to select
y = x(sort(randperm(numel(x),n)));
If you have an earlier version of MATLAB, then that last part must be done in multiple steps:
r = randperm(numel(x));
y = x(sort(r(1:n)));
  1 件のコメント
Shuo Zhang
Shuo Zhang 2018 年 1 月 26 日
This is quite amazing! Thank you very much!

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


Youssef  Khmou
Youssef Khmou 2018 年 1 月 26 日
The selection procedure can be performed by ordering the inputs from the smallest to the largets, here is an example, a vector of N=4000 samples, we select randomnly P=100 points where the described rule is valid:
N=4000;
P=100;
x=rand(1,N);
Index=sort(round(N*rand(1,P)));
figure;
bar(Index);
y=x(Index);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by