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

后端 未结 2 657
悲&欢浪女
悲&欢浪女 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))
    
    0 讨论(0)
  • 2020-12-06 06:54

    There are two approaches you can take. The first solution is to randomly pick k values from N values, which will ensure that you always have k points chosen. The second solution is to pick values randomly with each having an average probability p of being chosen, which could result in as little as 0 or as many as N being randomly chosen.

    • Picking k from N values:

      You can use the function RANDPERM to create a random permutation of the integers 1 through N, then pick the first k values in the permuted list and replot them as red:

      index = randperm(N);
      plot(x(index(1:k)),y(index(1:k)),'r*');
      
    • Picking values with an average probability p:

      You can use the RAND function to pick a random value from 0 to 1 for each of your N values, then choose the ones with a random value less than or equal to your average probability p and replot them as red:

      index = (rand(N,1) <= p);
      plot(x(index),y(index),'r*');
      
    0 讨论(0)
提交回复
热议问题