How to get random values from a matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
There is a matrix A (size:12x1000)
How can I select 5 random entries out of 1000( from a matrix of size 12x1000), with each randomly selected value contains all the 12 rows from the original matrix. And store it into another matrix of size 12x5.
Can anyone help me with the code.
댓글 수: 0
채택된 답변
Ameer Hamza
2020년 11월 10일
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
댓글 수: 0
추가 답변 (2개)
Bruno Luong
2020년 11월 10일
rcols = randperm(size(A,2),5);
B = A(:,rcols)
댓글 수: 2
Bruno Luong
2020년 11월 10일
I just interpret the question like this. I don't know if it's what OP wants or not.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!