MATLAB: How to get random values from a matrix

loopsmatrixmatrix arraymatrix manipulationrandomrandom number generator

There is a matrix A (size:12×1000)
How can I select 5 random entries out of 1000( from a matrix of size 12×1000), with each randomly selected value contains all the 12 rows from the original matrix. And store it into another matrix of size 12×5.
Can anyone help me with the code.

Best Answer

You can use randperm() in a for-loop
M = rand(12, 1000);
rand_elements = zeros(12, 5);
for i = 1:12
idx = randperm(1000, 5);
rand_elements(i,:) = M(i, idx);
end