Matlab中randsample的索引位置

时间:2016-06-22 23:22:33

标签: matlab random

我使用Matlab的randsample函数以下列方式对数据点进行采样:

points = randi(100,1,10);
weighting_vector = rand(1,10);
normalized_weighting_vector = weighting_vector ./ sum(weighting_vector);
point = randsample(points,1,'true',normalized_weighting_vector);

如何获取所选点的索引?

例如,如果points = [1,2,2,3,4,4,4,9,9]和point = 4,我想知道所选值的索引位置,它可以是是5,6或7。

1 个答案:

答案 0 :(得分:2)

不是在数据上使用randsample,而是随机抽取 indices ,然后将这些索引转换为points中的相应值。

points =  [1,2,2,3,4,4,4,8,9,3];

% Randomly choose N indices from the possible index values for points
index = randsample(1:numel(points), 1, true);

% Get the point corresponding to these indices
point = points(index)