MATLAB: How to randomly repeat an array elements

arraysMATLABrandom

I have a 1×4 array as,
P = [1, -1, j, -j]
How to form a New " 1×16 random array " using only four elements of 'P' ?
This new 1×16 array should have random arrangement of elements of 'P',(i.e.,irrespective of order of elements of 'P')

Best Answer

Here is an approach:
P = [1 -1 j -j]
N = 16 ;
ix = ceil(numel(P)*rand(1,N)) % random indices into P
Y = P(ix)
If you have access to RANDI, you could use that function as well.
If you want to have each element of P repeated four times, but all in random order, try this:
Y = repmat(P,1,4)
Y = Y(randperm(numel(Y)))