MATLAB: How to allocate specified number randomly without using randsrc

randrandsrc

If I use randsrc, I can simply allocate specified number.
alphabet = [-3 -1 1 3];
defaultip = randsrc(1,N,alphabet) + j*randsrc(1,N,alphabet);
But If I don't use that(i.e not using communication tool box), how can I code it?
Thanks for your help!

Best Answer

This is one of the way I can think of
alphabet = [-3 -1 1 3];
N = 50;
idx = randi([1 numel(alphabet)], 1, N);
defaultip = alphabet(idx);
Result:
>> defaultip
defaultip =
Columns 1 through 13
3 3 3 1 -3 -1 3 1 -3 1 -1 3 -1
Columns 14 through 26
-1 -1 3 -1 3 3 1 1 1 -1 3 -1 -3
Columns 27 through 39
-3 3 -3 3 -1 -1 -1 1 -3 -1 -3 -3 3
Columns 40 through 50
3 3 -3 3 1 -1 1 1 -3 -3 -1
Related Question