Generate numbers randomly from a set?

前端 未结 2 1661
执笔经年
执笔经年 2020-12-12 00:40

In MATLAB, I have a set of P numbers. I would like to generate a random array of size N from this set.

For the sake of example, let say I h

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 01:29

    If you don't have the Statistics Toolbox (which contains the datasample function), you can use randi:

    N = 5; %// desired number of samples
    data = [1 4]; %// data values
    sample = data(randi(numel(data),1,N));
    

    And if you use a very old version of Matlab that doesn't have randi, you can employ rand:

    sample = data(ceil(numel(data)*rand(1,N)));
    

提交回复
热议问题