How do I select random vectors from a set of vectors?
4 次查看(过去 30 天)
显示 更早的评论
Hello, I am very new with MATLAB and I apologize if this question has been answered before, but how does one select random vectors from a set of vectors? I have a series of vectors which I do some further analysis with. Instead of analyzing all 20 such vectors, I would like to analyze 5, 8, or 10 vectors in random sets. How would I go about generating these sets? Thank you very much in advance.
0 个评论
回答(2 个)
Jan
2013-8-7
Store the vectors in a cell array, or a matrix if possible. Then randperm allows to choose some of them:
data = rand(10, 5); % 10 vectors of size [1 x 5]
some = data(randperm(10, 3), :) % 3 of 10
or
data = {rand(1,5), rand(1,5), rand(1,5), rand(1,5), rand(1,5), ...
rand(1,5), rand(1,5), rand(1,5), rand(1,5), rand(1,5)};
some = data(randperm(10, 3));
for k = 1:numel(some)
disp(some{k})
end
0 个评论
Iain
2013-8-7
To get a random order:
random_numbers = rand(number_of_vectors,1);
[sorted_numbers random_order] = sort(random_numbers);
Now, you just need to use each element of random order to choose which vector you want. - Without more detail, thats hard to explain.
1 个评论
Jan
2013-8-7
编辑:Jan
2013-8-8
This exactly what in the old implementation of randperm happened. In newer Matlab versions, the 2nd input allows to create the wanted number of outputs more efficiently and the faster Fisher-Yates-Shuffle is used.
The old randperm (or your method) suffer from a tiny bias, because the random number generator can reply two equal numbers and Matlab's sort is stable, which means, that the order of equal elements is not changed. Although this effect is very very small, from the viewpoint of a computer scientist it is flawed and technically poor. But for real-world applications with just some million data sets, this is "as perfect as needed".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!