开发者

How would I randomly pick one point from N points in MATLAB?

开发者 https://www.devze.com 2022-12-12 15:50 出处:网络
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

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,'*')
hold on

开发者_开发问答How would I randomly pick one of these points?


You can use the function RANDI to generate a random integer in a given range:

index = randi(N);             %# Generate a random integer in the range 1 to N
plot(x(index),y(index),'o');  %# Plot the point

EDIT: As pointed out by Mikhail, the RANDI function has only been available since version 7.7 (R2008b). For earlier versions, the following alternative should work:

index = ceil(rand*N);


randnum=ceil(rand(1)*N)  %Sample a random integer in the range 0 to N
your_node = [x(randnum),y(randnum)] %Here is the sampled node from your data set

Edit: changed floor to ceil. 


Just take the first one. Being a product of the rand()-function, it should be random enough for anybody :-)

plot(x(1),y(1),'o');

0

精彩评论

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