Use cvpartition to generate a set of random 3 numbers 100 or 1000 times

1 visualizzazione (ultimi 30 giorni)
Hello Everyone
I need to generate random sets of data (three numbers) selected for numbers between 1 and 30. So I used
c = cvpartition(30,'KFold',3)
So, the result is always
TrainSize: 21 20 21
TestSize: 10 11 10
However, I need to repeat this 100 or 1000 times and get different TrainSize numbers each time.
Does anyone know how to:
  1. Prevent Matlab from giving the same numbers each time?
  2. Allow cvpartition to give me 100 or 1000 TrainSize numbers at the same time, instead of calling the function 100 or 1000 times?
Thank you.
Best,

Risposte (1)

the cyclist
the cyclist il 17 Mar 2023
Modificato: the cyclist il 17 Mar 2023
I don't think cvpartition does what you intend, and I am quite sure that you don't understand its output. Rather than explain cvpartition, here is my guess about what you intend, and an easier way to get it.
You want to choose 3 random numbers from the set 1:30, right? And you want to do this 100 times (or 1000, or whatever), right?
The method depends on whether you want to allow repeated values within each set of 3.
If repeated values are allowed, then
NG = 30;
NT = 7; % I'm using 7 instead of 100, so we can see all the results
N = 3;
x = randi(NG,NT,N) % Generate 7 sets of 3 values, REPEATS ALLOWED
x = 7×3
12 22 29 27 19 9 15 6 21 17 8 4 10 18 14 29 3 2 17 21 22
If repeated values are NOT allowed, then
x = zeros(NT,N);
for ni = 1:NT
x(ni,:) = randperm(NG,N);
end
x
x = 7×3
6 15 30 18 11 26 2 29 25 14 3 5 2 1 4 3 7 29 4 13 9
I couldn't remember or easily find a way to do the 2nd method without a loop, but it might exist.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by