One method is to use randperm so that you generate a random permutation of the n values ββthat are listed from 1 to and including n and return only the first two elements of the result:
ri = randperm(n, 2);
Older versions of MATLAB do not support calling randperm in this way. Older versions allow only one input option, which by default returns the entire permutation of n values. Therefore, you can call randperm with a single input version, then a subset of the final result to return what you need:
ri = randperm(n); ri = ri([1 2]);
source share