开发者

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

开发者 https://www.devze.com 2022-12-13 20:11 出处:网络
I use this co开发者_Python百科de to create and plot N points: N=input(\'No. of Nodes:\'); data = rand(N,2) % Randomly generated n no. of nodes

I use this co开发者_Python百科de 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 = data(:,2);
plot(x,y,'*');

How do I choose k points (with probability p=0.25) out of N points, then color those k points red and leave the other points as *.


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*');
    


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

精彩评论

暂无评论...
验证码 换一张
取 消