2015-10-18 37 views
-3
以產生從一個線段n個樣本

鑑於n個樣本100,我們如何生成下面用matlab如何利用Matlab

在線段這些隨機樣本

line_segement:-1和1之間

X ,Y = 2

+0

您是否想要在-1和1之間生成n個隨機樣本(值)? –

回答

1

如果你想生成(在你的問題-11)給定的極限之間n隨機樣本,您可以使用函數rand

這裏的示例:

% Define minimum x value 
x_min=-1 
% Define maximum x value 
x_max=1 
% Define the number of sample to be generated 
n_sample=100 
% Generate the samples 
x_samples = sort(x_min + (x_max-x_min).*rand(n_sample,1)) 

在該示例中,sort函數被調用,將值以具有ascendent系列進行排序。

x_min(x_max-x_min)用於「移動」的系列隨機值,使得它屬於所需的時間間隔(在此情況下-1 1)中,由於在一個開放的間隔(0,1)rand返回隨機數。

如果你想擁有由隨機抽樣和定義的常量Y值組成的XY矩陣(2):

y_val=2; 
xy=[x_samples ones(length(x_samples),1)*y_val] 

plot([x_min x_max],[y_val y_val],'linewidth',2) 
hold on 
plot(xy(:,1),xy(:,2),'d','markerfacecolor','r') 
grid on 
legend({'xy segment','random samples'}) 

(圖中,只有20個樣本已經情節,使之更加清晰)

enter image description here

希望這有助於。

+0

Thanks.This is very helpful –

+0

不客氣!快樂我一直在使用你。 –