Selecting a random element from a matrix with a range.
1 次查看(过去 30 天)
显示 更早的评论
I am trying to select a random element from this matrix with a certain range.
d = rand(10)
d(end,randperm(size(d,2), 1))
This creates a 10*10 matrix between 1 and 0. I want to slect a random variable < 0.5 but i am trouble applying this to the code. Any help is much appreciated.
2 个评论
Cedric
2017-10-23
There may not be any element < 0.5. How do you define the number of elements that you will pick? What is the purpose?
采纳的回答
Akira Agata
2017-10-24
How about the following script?
d = rand(10);
% Extract elements of d < 0.5
idx = d < 0.5;
d2 = d(idx);
% If d2 is NOT empty, select one element randomly
if ~isempty(d2)
output = d2(randperm(numel(d2),1));
end
更多回答(0 个)
另请参阅
类别
在 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!