使用randsample从两个向量进行采样

时间:2012-09-05 22:43:29

标签: matlab random random-sample

我有2个向量( n t ),例如:

n  t
1  5
5  3
5  2
2  6
2  9

一旦我从矢量 n randsample(n,1)进行采样,我想从矢量 t 中采样,但只能从矢量中相应的值中采样名词

例如。如果我从 n 中提取值2,那么我想从 t 中绘制值6或9。但是我怎么告诉matlab这样做呢?

2 个答案:

答案 0 :(得分:2)

您可以这样做:

out = t(n == randsample(n, 1))

这将根据n =自己的随机样本创建过滤器,即

randsample(n, 1) = 2
(n == randsample(n, 1)) = [0
                           0
                           0
                           1
                           1]

并将其应用于t ie:

 t(n == randsample(n, 1)) = [6
                             9]

n 中2的两个对应值,但 t

希望这有帮助。

PS如果您只需要一个t值,那么您可以对此函数提供的输出进行randsample。

答案 1 :(得分:1)

简单的单行,假设您将它们存储在Nx2矩阵

nt = [
1  5;
5  3;
5  2;
2  6;
2  9];

含义:

n = nt(:,1);
t = nt(:,2);

你可以通过randmonly为行矩阵索引来替换nSamples,即:

nSamples = 5;
keepSamples = nt(randi(length(nt),nSamples,1),:);
相关问题