【问题标题】:how to generate n samples from a line segment using Matlab如何使用Matlab从线段生成n个样本
【发布时间】:2015-10-18 16:10:56
【问题描述】:

给定n个100个样本,我们如何使用matlab在下面的线段中生成这些随机样本

line_segement:

x 在 -1 和 1 之间,y=2

【问题讨论】:

  • 你想生成 n 个介于 -1 和 1 之间的随机样本(值)吗?

标签: matlab random-sample line-segment


【解决方案1】:

如果您想在给定限制之间生成n 随机样本(在您的问题-11 中),您可以使用函数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),因为 rand 在开区间 @ 返回随机数987654334@.

如果你想要一个由随机样本和定义的常数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个样本,更清楚)

希望这会有所帮助。

【讨论】:

  • 谢谢。这很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 2017-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多