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!