How do I select random vectors from a set of vectors?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Jan
am 7 Aug. 2013
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 Kommentare
Iain
am 7 Aug. 2013
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 Kommentar
Jan
am 7 Aug. 2013
Bearbeitet: Jan
am 8 Aug. 2013
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".
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!