tags:

views:

367

answers:

2

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

+5  A: 

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*');
    
gnovice
thanks a lot, second answer is perfect for me.
gurwinder
+2  A: 

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))
Amro
thankx a lot , i also need those k chosen points, as u told data(~index,:)=[]; thankx for help
gurwinder
if we have to find distance between any point of data=rand(N,2) (say first) from any point of data(~index, :) = [];(say first of this also), how can we use data(~index, :) = []; matrix in following statement?dist=sqrt((data(1,1)-data(~index,1))^2+(data(1,2)-data(~index,2))^2).my question is as we are taking first element of data as data(1,1) and data (1,2). how do we take take first element of data(~index,:) matrix?
gurwinder
Im not quite sure I understand the question, but know that `data(~index,:)=[]` simply removes all the elements not selected. So if you plan to use both selected and non-selected points, you SHOULDNT remove them, instead you can perhaps store them in two different matrices: `selected = data(index,:);` and `not_selected = data(~index,:);` . Then if you want compute the distance between two points `i` and `j` each from one set, use: `dist_ij = sqrt(sum((selected(i,:)-not_selected(j,:)).^2));`
Amro
thankx again my friend, as u didn't response from here first, so posted it as another question. can i remove that question from board or let be there? thankx anyways
gurwinder
@amro selected = data(index,:); is giving me following error??? Index exceeds matrix dimensions.
gurwinder
now i got it thankx a lot amro u r genius bro :)
gurwinder

related questions