MATLAB: How can i use randperm without involving some certain numbers

randperm

For example i want to use randperm(5,3). But it should not include number 2. It can take any 3 values from 1 to 5, without including number 2.

Best Answer

Use randperm to compute a random permutation of indices into your value instead of a random permutation of your values:
values = [1 3 4 5]
randidx = randperm(numel(values));
randvalues = values(randidx)
%or as one line:
randvalues = values(randperm(numel(values)))