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

前端 未结 3 1083
自闭症患者
自闭症患者 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: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);
    

提交回复
热议问题