Generate numbers randomly from a set?

前端 未结 2 1664
执笔经年
执笔经年 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:18

    You should use datasample,

    y = datasample(data,k) returns k observations sampled uniformly at random, with replacement, from the data in data.

    a = [1,4];
    datasample(a,5)
    

    Depending on the usage, you might consider using,

    datasample(unique(a),5)
    

提交回复
热议问题