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

前端 未结 3 1071
自闭症患者
自闭症患者 2020-12-21 13:24

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


        
相关标签:
3条回答
  • 2020-12-21 14:11
    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. 
    
    0 讨论(0)
  • 2020-12-21 14:12

    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 讨论(0)
  • 2020-12-21 14:16

    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);
    
    0 讨论(0)
提交回复
热议问题