MATLAB: Problem with randsample when array with one element

randomStatistics and Machine Learning Toolbox

when vector has one element, the result of randsample is not correct for my problem. It returns a random number from 1 to that element.
for example:
n=[3];
x = randsample(n,1);
randsample returns x=1 or x=2 or x=3
but I want x=3 only

Best Answer

Perhaps you want
n = 3;
% get a random index no greater than the length of n:
ind = randsample(length(n),1);
% take a random value from n:
x = n(ind)