Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

matlab

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 = 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 *.

like image 307
gurwinder Avatar asked Dec 06 '09 18:12

gurwinder


2 Answers

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*');
    
like image 143
gnovice Avatar answered Oct 07 '22 15:10

gnovice


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))
like image 28
Amro Avatar answered Oct 07 '22 17:10

Amro