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