我使用此代码创建并绘制N
点:
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
我如何随机选择其中一个点?
答案 0 :(得分:9)
您可以使用函数RANDI生成给定范围内的随机整数:
index = randi(N); %# Generate a random integer in the range 1 to N
plot(x(index),y(index),'o'); %# Plot the point
编辑:正如Mikhail所指出的那样,RANDI功能自版本7.7(R2008b)起才可用。对于早期版本,以下替代方案应该有效:
index = ceil(rand*N);
答案 1 :(得分:2)
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.
答案 2 :(得分:0)
拿第一个。作为rand() - 函数的产物,对于任何人来说它应该是随机的: - )
plot(x(1),y(1),'o');