@David provides a neat solution for your specific problem. But the following solution will work for any set of numbers (note that they do not have to be integers), although I configured it for your specific problem.
OutputSize = [20, 1]; %Size of output matrix A = [-3; -2; -1; 1; 2; 3]; %Vector of numbers you want to sample from (can be whatever you want) x = A(randi(length(A), OutputSize)); %We randomly select the indices of A
You might want to enable the above functions with inputs A and OutputDim .
You can even reuse cell array elements with this method ...
EDIT: Corrected code allowing outputting an array of any size as suggested by @Divakar in the comments
source share