How do I randomly select k points from N points in MATLAB?

后端 未结 2 700
悲&欢浪女
悲&欢浪女 2020-12-06 06:27

I use this code to create and plot N points:

N=input(\'No. of Nodes:\');
data = rand(N,2) % Randomly generated n no. of nodes
x = data(:,1);
y =         


        
2条回答
  •  星月不相逢
    2020-12-06 06:36

    From what I understood, for each of the N random point you want to flip a coin to decide whether to select it or not (where the coin has a p=0.25 probability of success!)

    data = rand(N,2);             %# generate random points
    index = (rand(N,1) <= p);     %# roll coins to pick with prob p
    data(~index, :) = [];         %# keep only selected points
    

    This ends up being equivalent to only generating p*N random points in the first place (at least you approach this number as N grows larger)...

    data = rand(p*N, 2);          %# directly generate p*N number of points
    


    you can test that last statement for various values of N:

    fprintf('1st = %d \n', p*N)
    fprintf('2nd = %d \n', sum(rand(N,1) <= p))
    

提交回复
热议问题