randomly select elements of an array

Illustration
Amolisingh - 2020-07-28T12:54:45+00:00
Question: randomly select elements of an array

Hi How i can select randomly elements from a matrix o array I have the matrix and i want to select "x" numbers of elements

Expert Answer

Profile picture of Neeta Dsouza Neeta Dsouza answered . 2025-11-20

msize = numel(YourMatrix);
idx = randperm(msize);
YourMatrix(idx(1:x))

If you have a fairly new version of MATLAB, you can instead use

msize = numel(YourMatrix);
YourMatrix(randperm(msize, x))


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!