MATLAB: Choose elements from array randomly

choose element

Hi everyone
I have vector array of elements …
for example a=[1,2,3,4]
how can I choose randomly 10% of the above array????
Help me please
majid

Best Answer

You must be using an older version of MATLAB, do this instead:
% create vector
a = randn(100,1);
% determine how many elements is ten percent
numelements = round(0.1*length(a));
% get the randomly-selected indices
indices = randperm(length(a));
indices = indices(1:numelements);
% choose the subset of a you want
b = a(indices);